tf.Variable(initializer,name),参数initializer是初始化参数,name是可自定义的变量名称,用法如下:
import tensorflow as tf
v1=tf.Variable(tf.random_normal(shape=[4,3],mean=0,stddev=1),name='v1')
v2=tf.Variable(tf.constant(2),name='v2')
v3=tf.Variable(tf.ones([4,3]),name='v3')
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(v1))
print(sess.run(v2))
print(sess.run(v3))
结果如下:
[[-1.2115501 1.0484737 0.55210656]
[-1.5301195 0.9060654 -2.6766613 ]
[ 0.27101386 -0.32336152 0.44544214]
[-0.0120788 -0.3409422 -0.48505628]]
2
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]