pytorch 不知道如何定义多个模型

问题描述 投票:0回答:0

我想在pytorch中使用两种不同的模型。因此,我执行了下面的代码,但是我无法成功运行第二个模型。我该怎么做?

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.linear1 = nn.Linear(2, 64)
        self.linear2 = nn.Linear(64, 3)

    def forward(self, x):   
        x = self.linear1(x)
        x = torch.sigmoid(x)  
        x = self.linear2(x)

        return x
                
class Model2(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.linear1 = nn.Linear(2, 64)
        self.linear2 = nn.Linear(64, 1)

    def forward(self, x):   
        x = self.linear1(x)
        x = torch.sigmoid(x)  
        x = self.linear2(x)

        return x
                
net = Model()
net2 = Model2()

错误

TypeError Traceback(最后一次调用)

/tmp/ipykernel_477/2280223066.py in <module>
     26 
     27 net = Model()
---> 28 net2 = Model2()

/tmp/ipykernel_477/2280223066.py in __init__(self)
     14 class Model2(nn.Module):
     15     def __init__(self):
---> 16         super(Model, self).__init__()
     17         self.linear1 = nn.Linear(2, 64)
     18         self.linear2 = nn.Linear(64, 1)

TypeError: super(type, obj): obj 必须是 type 的实例或子类型

python deep-learning pytorch neural-network
© www.soinside.com 2019 - 2024. All rights reserved.