如何使用 PyTorch 组合两个经过训练的模型?

发布于 2025-01-11 22:09:11 字数 197 浏览 0 评论 0原文

我目前正在研究两个使用不同类型数据但相互连接的模型。我想创建一个组合模型,它接受每种数据类型的一个实例,独立地通过每个预训练模型运行它们,然后通过一些前馈处理两个不同模型的组合输出层在顶部。 到目前为止,我已经了解到我可以向前更改以接受两个输入,因此我只是将各个模型的结构克隆到组合模型中,使用前向(右)的层单独处理它们,然后合并指定的输出。我遇到的麻烦是弄清楚如何实现这一目标。

I'm currently working on two models that use different types of data but are connected. I'd want to create a combination model that takes in one instance of each of the data types, runs them through each of the pre-trained models independently, and then processes the combined output of the two distinct models through a few feed-forward layers at the top.
So far, I've learned that I can change forward to accept both inputs, so I've just cloned the structures of my individual models into the combined one, processed them each individually using forward(right )'s layers, and then merged the outputs as specified. What I'm having trouble with is figuring out how to achieve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方觉久 2025-01-18 22:09:11

据我从你的问题中了解到,你可以创建两个模型,然后你需要第三个模型,将神经网络与前向结合起来,然后在 __main__ 中你可以 load_state_dict
例如:

第一个模型

class FirstM(nn.Module):
    def __init__(self):
        super(FirstM, self).__init__()
        self.fc1 = nn.Linear(20, 2)
        
    def forward(self, x):
        x = self.fc1(x)
        return x

第二个模型

class SecondM(nn.Module):
    def __init__(self):
        super(SecondM, self).__init__()
        self.fc1 = nn.Linear(20, 2)
        
    def forward(self, x):
        x = self.fc1(x)
        return x

在这里您创建一个模型,您可以将其中的两个模型合并,如下所示:

class Combined_model(nn.Module):
    def __init__(self, modelA, modelB):
        super(Combined_model, self).__init__()
        self.modelA = modelA
        self.modelB = modelB
        self.classifier = nn.Linear(4, 2)
        
    def forward(self, x1, x2):
        x1 = self.modelA(x1)
        x2 = self.modelB(x2)
        x = torch.cat((x1, x2), dim=1)
        x = self.classifier(F.relu(x))
        return x

然后在主分类之外您可以执行以下操作

# Create models and load state_dicts    
modelA = FirstM()
modelB = SecondM()
# Load state dicts
modelA.load_state_dict(torch.load(PATH))
modelB.load_state_dict(torch.load(PATH))

model = Combined_model(modelA, modelB)
x1, x2 = torch.randn(1, 10), torch.randn(1, 20)
output = model(x1, x2)

as I understand from your question you can create two models then you need a third model that combines both the neural network with the forward and in the __main__ you can then load_state_dict
for example:

the first model

class FirstM(nn.Module):
    def __init__(self):
        super(FirstM, self).__init__()
        self.fc1 = nn.Linear(20, 2)
        
    def forward(self, x):
        x = self.fc1(x)
        return x

the second model

class SecondM(nn.Module):
    def __init__(self):
        super(SecondM, self).__init__()
        self.fc1 = nn.Linear(20, 2)
        
    def forward(self, x):
        x = self.fc1(x)
        return x

here you create a model that you can merge both two models in it as follows:

class Combined_model(nn.Module):
    def __init__(self, modelA, modelB):
        super(Combined_model, self).__init__()
        self.modelA = modelA
        self.modelB = modelB
        self.classifier = nn.Linear(4, 2)
        
    def forward(self, x1, x2):
        x1 = self.modelA(x1)
        x2 = self.modelB(x2)
        x = torch.cat((x1, x2), dim=1)
        x = self.classifier(F.relu(x))
        return x

and then outside the classed in the main you can do as following

# Create models and load state_dicts    
modelA = FirstM()
modelB = SecondM()
# Load state dicts
modelA.load_state_dict(torch.load(PATH))
modelB.load_state_dict(torch.load(PATH))

model = Combined_model(modelA, modelB)
x1, x2 = torch.randn(1, 10), torch.randn(1, 20)
output = model(x1, x2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文