1.RuntimeError: CUDA out of memory. Tried to allocate 20.00 MiB (GPU 0; 2.00 GiB total capacity; 1.15 GiB already all
ocated; 14.43 MiB free; 139.84 MiB cached)
这个错误的原因是cuda内存不足,一种解决办法是调整batch_size的大小,减小batch_size。batch_size的修改在option文件夹下的base_options.py中parser.add_argument('--batch_size', type=int, default=1, help='input batch size')
。而我是在自己的电脑上跑的,batch_size早就设置成1了,那显然不是这个原因。
经过查阅资料,我修改了models文件夹下的networks.py,原代码为return self.model(input)
,修改成with torch.no_grad(): return self.model(input)
,从而解决了问题。
2.RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
这个错误原因是变量默认requires_grad=False,在models文件夹下的cycle_gan_model.py中在原self.loss_G.backward()
的上一行加一行代码self.loss_G=Variable(self.loss_G, requires_grad=True)
,成功解决问题。