TypeError:‘tuple‘ object is not callable

报错:Type错误:元组不可调用 == 存在元组在调用参数; 

出现该错误就要考虑是不是有的地方写成元组了,也就是多半是逗号 “,” 的问题,删除逗号即可!
 

实例:

存在错误的代码:

class Net(nn.Module):
    def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):
        super(Net, self).__init__()

        self.linear1 = nn.Linear(in_dim, n_hidden_1),
        self.Relu1   = nn.ReLU(True),
        self.linear2 = nn.Linear(n_hidden_1, n_hidden_2),
        self.Relu2   = nn.ReLU(True),
        self.linear3 = nn.Linear(n_hidden_2, out_dim)

    def forward(self, x):

        x = self.linear1(x)
        x = self.Relu1(x)
        x = self.linear2(x)
        x = self.Relu2(x)
        x = self.linear3(x)

        return x

报错:Type错误:元组不可调用 == 存在元组在调用参数;

原因:在pytorch中定义网络的时候,将定义的网络层转变成元组了

解决方法:采用逐层定义网络而不是nn.sequential()进行定义,则需要把每一层定义后不添加逗号

正确的代码如下:

class Net(nn.Module):
    def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):
        super(Net, self).__init__()

        self.linear1 = nn.Linear(in_dim, n_hidden_1)
        self.Relu1   = nn.ReLU(True)
        self.linear2 = nn.Linear(n_hidden_1, n_hidden_2)
        self.Relu2   = nn.ReLU(True)
        self.linear3 = nn.Linear(n_hidden_2, out_dim)

    def forward(self, x):

        x = self.linear1(x)
        x = self.Relu1(x)
        x = self.linear2(x)
        x = self.Relu2(x)
        x = self.linear3(x)

        return x

Pytorch中:定义网络层的时候出现TypeError:'tuple' object is not callable_Kris-Allen的博客-CSDN博客