Session.run和Tensor.eval的区别和联系

Session.run()和Tensor.eval()

在会话中会需要运行节点,会碰到两种方式:
- Session.run()
- Tensor.eval()

1. 联系:

如果t是一个tf.Tensor对象,则tf.Tensor.eval是tf.Session.run的缩写(其中 的tf.Sesstion是tf.get_default_session)也就是:

tensor.eval()=tf.get_default_session().run(tensor)

2. 区别:

这两个中间最主要的区别就在于

使用sess.run()能在同一步获取多个tensor中的值,见上面例子x,y。

import tensorflow as tf

x = tf.ones(shape=[2, 3], dtype=tf.int32,name='x')
y= tf.zeros(shape=[2, 3], dtype=tf.float32,name='y')

with tf.Session() as sess:
    print(sess.run([xy]))   #一次能打印两个
    print(x.eval())
    print(y.eval()) #一次只能打印一个

参考博文:TensorFlow基础2:Session.run()和Tensor.eval()的区别

猜你喜欢

转载自blog.csdn.net/promisejia/article/details/80791626