Home > Blockchain >  PyTorch and Neural Networks: How many parameters in a layer?
PyTorch and Neural Networks: How many parameters in a layer?

Time:06-09

Ive seen many sources talk about the number of parameters in a neural network and mention that it is calculated as:

num parameters = ((shape of width of the filter * shape of height of the filter * number of filters in the previous layer 1)*number of filters)

but I've been having trouble understanding how that applies to networks created using nn from torch

for example how many parameters would this network have?

class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.flatten = nn.Flatten()
        self.linear_relu_stack = nn.Sequential(
            nn.Linear(28*28, 512),
            nn.ReLU(),
            nn.Linear(512, 512),
            nn.ReLU(),
            nn.Linear(512, 10)
        )

    def forward(self, x):
        x = self.flatten(x)
        logits = self.linear_relu_stack(x)
        return logits

CodePudding user response:

The object nn.Linear represents a matrix with dimention [m, n]. For example, nn.Linear(28*28, 512) has (28*28)*512 parameters(weights). Check here for more information about it.

The object nn.Flatten() and nn.ReLU() do not contain parameters.

  • Related