Parameters Torch7,如何计算convNet中的参数数
我正在寻找一种方法来计算卷积神经网络中的参数数量。特别是,我在中使用了Resnet模型。 您知道是否有任何函数可以计算参数的总数吗?你还有其他建议吗?Parameters Torch7,如何计算convNet中的参数数,parameters,torch,conv-neural-network,Parameters,Torch,Conv Neural Network,我正在寻找一种方法来计算卷积神经网络中的参数数量。特别是,我在中使用了Resnet模型。 您知道是否有任何函数可以计算参数的总数吗?你还有其他建议吗? 提前谢谢 基本上,您必须遍历网络的每一层,并计算该层中的参数数量。下面是一个示例函数: -- example model to be fed to the function model = nn.Sequential() model:add(nn.SpatialConvolution(3,12,1,1)) model:add(nn.Linear(
提前谢谢 基本上,您必须遍历网络的每一层,并计算该层中的参数数量。下面是一个示例函数:
-- 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
如果要在
torch
中训练网络,必须首先提取其参数向量和梯度向量w.r.t。这些参数(都是1D张量):
完成后,很容易获得可学习的参数数:
n_params = params:size(1)
除了已经回答的问题之外,如果您只想在层级别计算网络的参数数量,那么最好使用
params, gradParams = net:parameters()
print(#params)
而不是getParameters()
(返回平坦的长张量)
函数
parameters()
在您需要分层设置不同的学习速率时非常有用。我认为有一个特定的函数可以实现这一点,但我找不到。非常感谢你的帮助!如果你觉得这个答案有帮助,你介意把它标记为正确答案吗?这会提高我的声誉。
params, gradParams = net:parameters()
print(#params)