【Tensorflow】打印输出tensor张量和变量的方法

打印输出张量的值的方法

import tensorflow as tf

zeros = tf.zeros([3,3])

# 方法1
with tf.Session():
    print(zeros.eval())

# 方法2
sess = tf.Session()
print(sess.run(zeros))

打印输出tensor变量的值的方法

import tensorflow as tf

ones=tf.Variable(tf.ones([3,3]))

# 方法1 InteractiveSession + initializer
inter_sess=tf.InteractiveSession()
ones.initializer.run()
print(inter_sess.run(ones))

# 方法2
inter_sess=tf.InteractiveSession()
tf.global_variables_initializer().run()
print(inter_sess.run(ones))

# 方法3 Session + global_variables_initializer
sess=tf.Session()
sess.run(tf.global_variables_initializer())
print(sess.run(ones))

# 方法4 with Session + global_variables_initializer
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(ones))

你可能感兴趣的:(解决问题,#Tensorflow)