How to count the number of multiplications given a keras model?
See original GitHub issueHi,
I am wondering how to count the number of multiplications given a keras model, which is well defined. Is there any off-the-shelf function or codes?
I know obtaining the number of parameters given a keras model is to use model.summary()
.
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (2 by maintainers)
Top Results From Across the Web
How to find the number of operation ( multiplication or addition ...
The question is given in the model above, how many mathematical operations (plus, minus, multiplication, division) are required to find the ...
Read more >How to count Multiply-Adds operations? - tensorflow
Counting the Multiply-Add operations is equivalent to calculating the FLOPs of a model. This can be achieved using the profiler from ...
Read more >[D] Precisely count the number of operations in Keras model
I'm working in a tool to predict the inference speed of a model, based on input shape and model definition, i want to...
Read more >Calculating Parameters of Convolutional and Fully Connected ...
Calculating Parameters of Convolutional and Fully Connected Layers with Keras. Explain how to calculate the number of params and output shape of convolutional ......
Read more >How fast is my model? - Machine, Think!
When calculating FLOPS we usually count addition, subtraction, multiplication, division, exponentiation, square root, etc as a single FLOP.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Is there any solution for this?
I had the same problem. Here’s my solution. It turns out that for a dense layer the number of multiplications should be the same as the number of weights. But this does not work as well for the convolution layers. For a convolution layer you need to multiply the number of weights by the number of instances of the kernel applied on the input to get the total number of multiplications. If my assumptions are right, then this should work for dense, conv and separable conv layers:
import numpy as np
/# here you need to create the model
print(‘\n’.join([ 'layer: ’ + type(l).name +
', output shape: ’ + str(l.output_shape[1:]) +
', weights: ’ + str(sum([i.size for i in l.get_weights()])) +
', multiplications: ’ + str(sum([i.size for i in l.get_weights()]) * int(np.prod(l.output_shape[1: -1])))
for l in model.layers ]))
The output for my CNN (note that you should, at least temporarily, define the input shape):
layer: SeparableConv2D, output shape: (240, 240, 128), weights: 400, multiplications: 23040000 layer: BatchNormalization, output shape: (240, 240, 128), weights: 512, multiplications: 29491200 layer: SeparableConv2D, output shape: (80, 80, 256), weights: 51456, multiplications: 329318400 layer: BatchNormalization, output shape: (80, 80, 256), weights: 1024, multiplications: 6553600 layer: SeparableConv2D, output shape: (40, 40, 256), weights: 90368, multiplications: 144588800 layer: BatchNormalization, output shape: (40, 40, 256), weights: 1024, multiplications: 1638400 layer: Dropout, output shape: (40, 40, 256), weights: 0, multiplications: 0 layer: SeparableConv2D, output shape: (40, 40, 256), weights: 84224, multiplications: 134758400 layer: Dropout, output shape: (40, 40, 256), weights: 0, multiplications: 0 layer: Conv2D, output shape: (40, 40, 5), weights: 92165, multiplications: 147464000
It does not actually work for BatchNormalization layers, so the output for those layers probably is meaningless.
P.S. idk how to add code properly