[Tensorflow] 参数保存剖析

在tf.global_variables() 里可以看到所有参数
print( tf.global_variables() )
print( sess.run(tf.global_variables() ) 显示其中的数值

这里的名称 'Layer1' 是通过tf.variable_scope() 设置, weights是variable的name,
[<tf.Variable 'Layer1/weights:0' shape=(3072, 120) dtype=float32_ref>,
 <tf.Variable 'Layer1/biases:0' shape=(120,) dtype=float32_ref>, 
<tf.Variable 'Layer2/weights:0' shape=(120, 10) dtype=float32_ref>, 
<tf.Variable 'Layer2/biases:0' shape=(10,) dtype=float32_ref>,
 <tf.Variable 'global_step:0' shape=() dtype=int32_ref>]

关于 :0 官方没有明确解释
As far as I know it means “variable bar after 0th iteration” (stackoverflow)

其中variable有许多参数

demo_weights = tf.get_variable(
      name='weights',
      shape=[128, 64],
      initializer=tf.truncated_normal_initializer(
        stddev=1.0 / np.sqrt(float(128))
    ))

print("demo_weights")
print(demo_weights)
print(demo_weights.name)
print(demo_weights.initial_value)
print(demo_weights.dtype)
print(demo_weights.shape)
print(demo_weights.initializer)

所以如何部分载入参数呢

这里 v2, v3 都是variables , 可以通过在tf.global_variables 或其他方式找需要载入的参数
saver = tf.train.Saver([v2]+[v3])
saver.restore(sess, ckpt)

猜你喜欢

转载自blog.csdn.net/transMaple/article/details/82085189