[Tensorflow] 参数保存剖析

博客围绕TensorFlow展开,提到可通过tf.global_variables()查看所有参数,用sess.run(tf.global_variables())显示参数数值,对于参数后的:0官方未明确解释,最后提出如何部分载入参数的问题。
部署运行你感兴趣的模型镜像

在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)

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值