1,代码中实现可以输出变化的变量
print('#training images = %d' % dataset_size)
2,RuntimeError: The size of tensor a (32) must match the size of tensor b (28) at non-singleton
在修改卷积层的过程中,没有考虑到卷积层的输出受到输入图片大小、卷积核大小、步长和padding的影响。
N=(W-F+2P)/S+1
W:输入图片大小
F:卷积核大小
P:padding
S:步长
3,RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.
将下面语句
latent_res = latent.view(N, self.opt.G_s+self.opt.G_n, -1)
改为如下,此处添加.contiguous()
latent_res = latent.contiguous().view(N, self.opt.G_s+self.opt.G_n, -1)
为什么:
因为历史上view
方法已经约定了共享底层数据内存,返回的Tensor底层数据不会使用新的内存,如果在view
中调用了contiguous
方法,则可能在返回Tensor底层数据中使用了新的内存,这样打破了之前的约定,破坏了对之前的代码兼容性。为了解决用户使用便捷性问题,PyTorch在0.4版本以后提供了reshape
方法,实现了类似于 tensor.contigous().view(*args)
的功能,如果不关心底层数据是否使用了新的内存,则使用reshape
方法更方便。