How to use Torchsummary with multiple parameters

overview

  • Torchsummary is a tool commonly used in deep learning to describe the network structure and parameters of each layer. When building a network model, we can use it to check whether the parameters in the network model are correct, and further, whether the output information of the correct dimension can be given.
  • For the input information of the network model, in addition to the traditional single input, there are also multi-input network models. When multiple inputs are used, an error message will appear when using torchsummary: TypeError: can't multiply sequence by non-int of type 'tuple'
  • The version used in this case Torchsummary=1.5.1
  • reference:
    1. https://github.com/sksq96/pytorch-summary/issues/90
    2. https://blog.csdn.net/qq_43733107/article/details/126508616

question

According to the error message, you can locate the error code in Line 100 of torchsummary/torchsummary.py:

total_input_size = abs(np.prod(input_size) * batch_size * 4. / (1024 ** 2.))

Among them input_sizeis the dimension of the input information, such as common image information: (3, 64, 64). And when there are multiple inputs, it is impossible to directly np.prodrealize the multiplication of parameters.

Solution

method one

total_input_size = abs(np.prod(sum((input_size),())) * batch_size * 4. / (1024 ** 2.))

Method Two

total_input_size = abs(np.sum([np.prod(in_tuple) for in_tuple in input_size]) * batch_size * 4. / (1024 ** 2.))

After annotating the original code, you can choose one of the above two methods to realize the function. It can also be seen from the code that one is multiplication after addition, and the other is accumulation after traversal multiplication.

Guess you like

Origin blog.csdn.net/kakangel/article/details/130795893