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
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值