首页 > 编程 > Python > 正文

pytorch 在sequential中使用view来reshape的例子

2019-11-25 11:55:57
字体:
来源:转载
供稿:网友

pytorch中view是tensor方法,然而在sequential中包装的是nn.module的子类,

因此需要自己定义一个方法:

import torch.nn as nnclass Reshape(nn.Module): def __init__(self, *args):  super(Reshape, self).__init__()  self.shape = args def forward(self, x):  # 如果数据集最后一个batch样本数量小于定义的batch_batch大小,会出现mismatch问题。可以自己修改下,如只传入后面的shape,然后通过x.szie(0),来输入。  return x.view(self.shape)
class Reshape(nn.Module): def __init__(self, *args):  super(Reshape, self).__init__()  self.shape = args def forward(self, x):  return x.view((x.size(0),)+self.shape)

以上这篇pytorch 在sequential中使用view来reshape的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表