Total amount of weights in a layer

25 views
Skip to first unread message

Nicola Fiorato

unread,
Dec 11, 2017, 1:01:53 AM12/11/17
to Caffe Users
Does someone know how to get the total number of weights in a generic layer?

Przemek D

unread,
Dec 14, 2017, 9:23:16 AM12/14/17
to Caffe Users
From the C++ point of view, each layer has a blobs() method which returns vector of all Blobs that this layer can learn. You can then call Blob::count() on each of those to find the number of elements it contains.

Within the Python interface, let's assume you start with something like this:
n = caffe.Net('model.prototxt', caffe.TEST)
Then you can find all parameters in
n.params
which is an OrderedDict containing lists of parameter blobs for each layer. So under
n.params[key]
you will get the list of params (e.g. conv filters and biases), each stored as a Blob object. To get convenient access to the contents in form of a numpy array, do
n.params[key][index].data
From there the following gives you shape of the 1st blob of layer conv1:
n.params['conv1'][0].data.shape
and this counts how many weights are in there
n.params['conv1'][0].data.size

HTH!

Nicola Fiorato

unread,
Dec 17, 2017, 11:41:20 AM12/17/17
to Caffe Users
Thank you a lot, your answer fullfills what I needed (I'm using C++ btw). Great and simple method!!
Reply all
Reply to author
Forward
0 new messages