pytorch 在sequential中使用view来reshape的例子
pytorch中view是tensor方法,然而在sequential中包装的是nn.module的子类,
因此需要自己定义一个方法:
importtorch.nnasnn classReshape(nn.Module): def__init__(self,*args): super(Reshape,self).__init__() self.shape=args defforward(self,x): #如果数据集最后一个batch样本数量小于定义的batch_batch大小,会出现mismatch问题。可以自己修改下,如只传入后面的shape,然后通过x.szie(0),来输入。 returnx.view(self.shape)
classReshape(nn.Module): def__init__(self,*args): super(Reshape,self).__init__() self.shape=args defforward(self,x): returnx.view((x.size(0),)+self.shape)
以上这篇pytorch在sequential中使用view来reshape的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。