Skip to content
Advertisement

Giving output of one neural network as an input to another in pytorch

I have a pretrained convolution neural network which produces and output of shape (X,164) where X is the number of test examples. So output layer has 164 nodes. I want to take this output and give this two another network which is simply a fully connected neural network whereby the first layer has 64 nodes and output layer has 1 node with sigmoid function. How can I do that? My first network looks like:

JavaScript

If I want to get output for this model on my test set I can simply do:

JavaScript

This produces a final output of shape (X,164). Now I want to take this output and give it to another neural network mentioned above. How can I combine these two networks now and how can I optimise these networks together? Insights will be appreciated.

Edit: My second model is:

JavaScript

And my classifier is trained as:

JavaScript

Advertisement

Answer

If the two models do not need any adapting to be done at the first’s model output, you can simply use a nn.Sequential:

JavaScript

And use it the same way as you did with model:

JavaScript

Which will correspond to next_model(model(X.float())).

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement