如何使用 PyTorch 组合两个经过训练的模型?
我目前正在研究两个使用不同类型数据但相互连接的模型。我想创建一个组合模型,它接受每种数据类型的一个实例,独立地通过每个预训练模型运行它们,然后通过一些前馈处理两个不同模型的组合输出层在顶部。 到目前为止,我已经了解到我可以向前更改以接受两个输入,因此我只是将各个模型的结构克隆到组合模型中,使用前向(右)的层单独处理它们,然后合并指定的输出。我遇到的麻烦是弄清楚如何实现这一目标。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我从你的问题中了解到,你可以创建两个模型,然后你需要第三个模型,将神经网络与前向结合起来,然后在
__main__
中你可以load_state_dict
例如:
第一个模型
第二个模型
在这里您创建一个模型,您可以将其中的两个模型合并,如下所示:
然后在主分类之外您可以执行以下操作
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 thenload_state_dict
for example:
the first model
the second model
here you create a model that you can merge both two models in it as follows:
and then outside the classed in the main you can do as following