张量分解卷积层代码搭建错误修改(pytorch)

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方法更方便。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值