如何计算张量流模型中可训练参数的总数? [英] How to count total number of trainable parameters in a tensorflow model?
问题描述
是否有函数调用或其他方法来计算张量流模型中的参数总数?
Is there a function call or another way to count the total number of parameters in a tensorflow model?
按参数,我的意思是:可训练变量的N个暗矢量具有N个参数,NxM
矩阵具有N*M
个参数,依此类推.因此,本质上我想对所有可训练变量的形状尺寸乘积求和张量流会话中的变量.
By parameters I mean: an N dim vector of trainable variables has N parameters, a NxM
matrix has N*M
parameters, etc. So essentially I'd like to sum the product of the shape dimensions of all the trainable variables in a tensorflow session.
推荐答案
环顾tf.trainable_variables()
中每个变量的形状.
Loop over the shape of every variable in tf.trainable_variables()
.
total_parameters = 0
for variable in tf.trainable_variables():
# shape is an array of tf.Dimension
shape = variable.get_shape()
print(shape)
print(len(shape))
variable_parameters = 1
for dim in shape:
print(dim)
variable_parameters *= dim.value
print(variable_parameters)
total_parameters += variable_parameters
print(total_parameters)
更新:由于这个答案,我写了一篇文章来澄清Tensorflow中的动态/静态形状:
Update: I wrote an article to clarify the dynamic/static shapes in Tensorflow because of this answer: https://pgaleone.eu/tensorflow/2018/07/28/understanding-tensorflow-tensors-shape-static-dynamic/
这篇关于如何计算张量流模型中可训练参数的总数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!