tf.Variable()函数
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))
结果:
[[-0.3561664 -0.3963107 -2.544983 ]
[-0.5363636 0.4613348 -0.37325048]
[-0.29214492 1.6646444 -2.0157301 ]
[ 0.2745275 1.9994936 1.0050457 ]]
2
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]