Pytorch遇到的一些问题以及解决方案

Traceback (most recent call last):
  File "DCGAN.py", line 241, in <module>
    for x,y in enumerate(dataloader,0):
  File "/home/rancho/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 286, in __next__
    return self._process_next_batch(batch)
  File "/home/rancho/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 307, in _process_next_batch
    raise batch.exc_type(batch.exc_msg)
RuntimeError: Traceback (most recent call last):
  File "/home/rancho/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 57, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/home/rancho/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in default_collate
    return [default_collate(samples) for samples in transposed]
  File "/home/rancho/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in <listcomp>
    return [default_collate(samples) for samples in transposed]
  File "/home/rancho/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 115, in default_collate
    return torch.stack(batch, 0, out=out)
RuntimeError: cat(): functions with out=... arguments don't support automatic differentiation, but one of the arguments requires grad.

这里错误大概是在提示有的Tensor不该具有requires_grad = True的属性
在具体的问题中是我的Dataset类中__getitem__这个函数返回的第一个训练数据是由另一个模型generated出来的,所以说这个属性值为True,当我用如下方式改变这个属性后就不报错了:

    def __getitem__(self, index):
        # TODO
        # 1. Read one data from file (e.g. using numpy.fromfile, PIL.Image.open).
        # 2. Preprocess the data (e.g. torchvision.Transform).
        # 3. Return a data pair (e.g. image and label).
        # if(self.noise == None):
        #     print('Fatal error!!!')
        # assert self.noise == None
        self.count += 1
        random_noise = self.noise[index:index+1,:,:,:]
        generated = self.G(random_noise)
        #############################添加了这两句##################################
        generated = torch.autograd.Variable(generated,requires_grad = False)
        generated = generated.data

        return generated,random_noise

你可能感兴趣的:(pytorch)