TensorFlow学习2:tf.Variable()函数

当你需要一个可以修改的tensor的时候,就不能使用tf.paaceholder()和tf.constant()了,这时候需要tf.Variable()函数。

tf.Variable(initializer, name):initializer是初始化参数,可以有tf.random_normal,tf.constant,tf.constant等,name就是变量的名字,用法如下:

import tensorflow as tf;    
import numpy as np;    
import matplotlib.pyplot as plt;    
    
a1 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a1')  
a2 = tf.Variable(tf.constant(1), name='a2')  
a3 = tf.Variable(tf.ones(shape=[2,3]), name='a3')  
  
with tf.Session() as sess:  
    sess.run(tf.initialize_all_variables())  
    print sess.run(a1)  
    print sess.run(a2)  
    print sess.run(a3)  
输出:

[[ 0.76599932  0.99722123 -0.89361787]
 [ 0.19991693 -0.16539733  2.16605783]]
1
[[ 1.  1.  1.]
 [ 1.  1.  1.]]


猜你喜欢

转载自blog.csdn.net/Softdiamonds/article/details/80333297