Search code examples
parameterstorchconv-neural-network

Torch7, how to calculate the number of parameters in a convNet


I am looking for a way to calculate the number of parameters in a convolutional Neural Network. In particular, I am using the Resnet model in https://github.com/facebook/fb.resnet.torch. Do you know if there is any function that could calculate the total number of parameters? Do you have other suggestion for doing it? Thanks in advance.


Solution

  • You basically have to go through each layer of your network and count the number of parameters in that layer. Here is a sample function that does that:

    -- example model to be fed to the function
    model = nn.Sequential()
    model:add(nn.SpatialConvolution(3,12,1,1))
    model:add(nn.Linear(2,3))
    model:add(nn.ReLU())
    
    function countParameters(model)
    local n_parameters = 0
    for i=1, model:size() do
       local params = model:get(i):parameters()
       if params then
         local weights = params[1]
         local biases  = params[2]
         n_parameters  = n_parameters + weights:nElement() + biases:nElement()
       end
    end
    return n_parameters
    end