1、tf.Variable()
输入:
a = tf.Variable(initial_value = 1, trainable = True)
输出:
<tf.Variable 'Variable:0' shape=() dtype=int32_ref>
定义变量后,可以再赋值:
a = tf.ones((2,2))
2、tf.get_variable()
tf.get_variable 可以实现变量重复使用。
import tensorflow as tf
with tf.variable_scope("scope1"):
w1 = tf.get_variable("w1", shape=[])
w2 = tf.Variable(0.0, name="w2")
with tf.variable_scope("scope1", reuse=True):
w1_p = tf.get_variable("w1", shape=[])
w2_p = tf.Variable(1.0, name="w2")
print(w1 is w1_p, w2 is w2_p)
#输出
#True False
3、tf.variable.scope()
同2。
参考: