Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a neural network that has multiple outputs using pytorch?

Tags:

pytorch

Is my question even right? I looked everywhere but couldn't find a single thing. I'm pretty sure this was addressed when I learned keras, but how do I implement it in pytorch?

like image 208
Ahmed Samir Avatar asked Mar 12 '19 18:03

Ahmed Samir


People also ask

Can neural networks have multiple outputs?

Neural Networks for Multi-OutputsNeural network models also support multi-output regression and have the benefit of learning a continuous function that can model a more graceful relationship between changes in input and output.


1 Answers

Multiple outputs can be trivially achieved with pytorch.

Here is one such network.

import torch.nn as nn

class NeuralNetwork(nn.Module):
  def __init__(self):
    super(NeuralNetwork, self).__init__()
    self.linear1 = nn.Linear(in_features = 3, out_features = 1)
    self.linear2 = nn.Linear(in_features = 3,out_features = 2)

  def forward(self, x):
    output1 = self.linear1(x)
    output2 = self.linear2(x)
    return output1, output2
like image 54
Haran Rajkumar Avatar answered Oct 27 '22 23:10

Haran Rajkumar