介绍TensorFlow中的变量管理。
相关链接
变量管理
本文主要介绍tf.get_variable()
和tf.variable_scope()
来进行变量管理。
两种变量创建方式
tf.Variable()
的函数原型:
1 | __init__( |
tf.get_variable()
的函数原型:
1 | get_variable( |
常见创建方式:
1 | import tensorflow as tf |
v1,v2结构如下:
1 | <tf.Variable 'v1:0' shape=(1,) dtype=float32_ref> |
以上两种定义方式是等价的
tf.get_variable()
函数中初始化器列表,见官网
tf.get_variable()
中name
为必填字段
结合variable_scope
使用命名空间创建变量,且重复获得变量的代码如下:
1 | import tensorflow as tf |
结合variable_scope嵌套使用
代码如下:
1 | import tensorflow as tf |
输出如下:
1 | False |