我怎么知道“偏见"是否存在?存在于一层中? [英] How can i know whether "bias" exists in a layer?
问题描述
我正在尝试通过pycaffe读取caffe网络中的体重和偏见. 这是我的代码
I'm trying to read weight and bias in a caffe network with pycaffe. Here is my code
weight = net.params[layer_name][0].data
bias = net.params[layer_name][1].data
但是,我的网络中的某些层没有偏差,因此会出现错误Index out of range
.
But, some layers in my network has no bias, so that there will be an error which is Index out of range
.
所以我的问题是我可以使用
So my question is can I use
if(net.params[layer_name][1] exists):
bias = net.params[layer_name][1].data
控制对bias
的分配?
以及如何编写代码?
to control the assignments to bias
?
And how to write the code?
推荐答案
您可以简单地遍历net.params[layer_name]
:
layer_params = [blob.data for blob in net.params[layer_name]]
这样,您将获得全部layer_params
(对于某些图层,它可能大于2,例如, )
This way, you get all layer_params
(which might be more than 2 for some layers, e.g., "BatchNorm"
)
如果只想检查第二个参数blob,则可以使用len
:
If you only want to check for the second parameters blob, you can use len
:
if len(net.params[layer_name]) >= 2:
bias = net.params[layer_name][1].data
PS,
可能net.params[layer_name]
并非完全是python list
,而是某些python boost wrapper对象,因此您可能需要在我在此建议的某些方法中将其显式转换为列表(list(net.params[layer_name])
).答案.
PS,
It might be the case that net.params[layer_name]
is not exactly a python list
, but rather some python boost wrapper object, thus you might need to explicitly cast it to list (list(net.params[layer_name])
) in some of the methods I suggested in this answer.
这篇关于我怎么知道“偏见"是否存在?存在于一层中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!