在Pytorch中构建CNN时无法调用元组对象

时间:2019-04-26 20:27:26

标签: conv-neural-network pytorch

我是神经网络的新手,目前正在尝试构建具有2个转换层的CNN。

class CNN(nn.Module):
  def __init__(self):
    super(CNN, self).__init__()
    self.conv1 = nn.Conv2d(in_channels = 1, out_channels = 16, kernel_size = 3, stride = 1, padding = 1), 
    self.maxp1 = nn.MaxPool2d(2),
    self.conv2 = nn.Conv2d(in_channels = 16, out_channels = 16, kernel_size = 3, stride = 1, padding = 1),
    self.fc1 = nn.Linear(16, 64),
    self.fc2 = nn.Linear(64, 10)

  def forward(self, x):
    x = nn.ReLU(self.maxp1(self.conv1(x)))
    x = nn.ReLU(self.maxp2(self.conv1(x)))
    x = x.view(x.size(0), -1)
    x = nn.ReLu(self.fc1(x))
    return self.fc2

我想做的是ConvLayer-ReLu激活-最大池2x2-ConvLayer-ReLu激活-展平层-完全连接-ReLu-完全连接

但是,这给了我TypeError: 'tuple' object is not callable上的x = nn.ReLU(self.maxp1(self.conv1(x)))

我该如何解决?

1 个答案:

答案 0 :(得分:0)

  

您可以将override init(frame: CGRect) { super.init(frame: frame) setUpGradient() } 更改为nn.ReLU

如果您想使用F.relu,则最好将其声明为nn.ReLU()方法的一部分,并稍后在__init__中进行调用:

forward()