简体   繁体   English

如何连接 pytorch 中的模型?

[英]how can I connect models in pytorch?

I made 2 classes:我做了2节课:

class Preprocess():
    def __init
     .....
    def forward 
     ....

class Model ():
    def __init
     .....
    def forward 

I would like to do Preprocess before putting them into Model.我想在将它们放入 Model 之前进行预处理。 Could you kindly tell me how can I achieve that?你能告诉我我怎样才能做到这一点吗?

Just compose them:只需编写它们:

pre = Preprocess()
model = Model()

output = model(pre(input))

One could also use torch.nn.Sequential which has the following upsides:也可以使用torch.nn.Sequential ,它具有以下优点:

  • easier on the eyes眼睛更容易
  • one has to just pass the inputs instead of remembering to call Preprocess before it必须只传递inputs ,而不是记住在它之前调用Preprocess

Code being:代码是:

model = torch.nn.Sequential(Preprocess(), Model())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM