所谓的参数量,其实就是可训练的所有张量,每一个张量有多少位数字,然后统计一下数字的个数。
比如我们的网络中一共有两个可训练的张量,一个的形状是[5,3,2],另外一个是[5,2],网络的参数量就是:
从checkpoint中计算参数量的代码如下:
ckpt = tf.train.get_checkpoint_state("./ckpt_path/").model_checkpoint_path
saver = tf.train.import_meta_graph(ckpt+'.meta')
variables = tf.trainable_variables()
total_parameters = 0
for variable in variables:
shape = variable.get_shape()
variable_parameters = 1
for dim in shape:
# print(dim)
variable_parameters *= dim.value
# print(variable_parameters)
total_parameters += variable_parameters
print(total_parameters)
注意因为仅仅是计算参数的量,不需要参数的具体值,所以没有开启会话,只加载了图。