Quantcast
Viewing all articles
Browse latest Browse all 25

Why listing model components in pyTorch is not useful?

I am trying to create Feed forward neural networks with N layers So idea is suppose If I want 2 inputs 3 hidden and 2 outputs than I will just pass [2,3,2] to neural network class and neural network model will get created so if I want [100,1000,1000,2] where in this case 100 is inputs, two hidden layers contains 1000 neuron each and 2 outputs so I want fully connected neural network where I just wanted to pass list which contains number of neuron in each layer. So for that I have written following code

class FeedforwardNeuralNetModel(nn.Module):
    def __init__(self, layers):
        super(FeedforwardNeuralNetModel, self).__init__()
        self.fc=[]
        self.sigmoid=[]
        self.activationValue = []
        self.layers = layers
        for i in range(len(layers)-1):
            self.fc.append(nn.Linear(layers[i],layers[i+1]))
            self.sigmoid.append(nn.Sigmoid())

    def forward(self, x):
        out=x
        for i in range(len(self.fc)):
            out=self.fc[i](out)
            out = self.sigmoid[i](out)
        return out    

when I tried to use it I found it kind of empty model

model=FeedforwardNeuralNetModel([3,5,10,2])

print(model)

>>FeedforwardNeuralNetModel()

and when I used following code

class FeedforwardNeuralNetModel(nn.Module):
    def __init__(self, input_dim, hidden_dim, output_dim):
        super(FeedforwardNeuralNetModel, self).__init__()
        # Linear function
        self.fc1 = nn.Linear(input_dim, hidden_dim) 
        # Non-linearity
        self.tanh = nn.Tanh()
        # Linear function (readout)
        self.fc2 = nn.Linear(hidden_dim, output_dim)  

    def forward(self, x):
        # Linear function
        out = self.fc1(x)
        # Non-linearity
        out = self.tanh(out)
        # Linear function (readout)
        out = self.fc2(out)
        return out

and when I tried to print this model I found following result

print(model)

>>FeedforwardNeuralNetModel(
(fc1): Linear(in_features=3, out_features=5, bias=True)
(sigmoid): Sigmoid()
(fc2): Linear(in_features=5, out_features=10, bias=True)
)

in my code I am just creating lists that is what difference I just wanted to understand why in torch listing model components is not useful?


Viewing all articles
Browse latest Browse all 25

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>