tensorflow中,Session.run和Tensor.eva的区别在于前者一次可以获得多个tensor值

之前在TensorFlow中运行代码时,在会话中会需要运行节点,会碰到两种方式:Session.run()和Tensor.eval(),刚开始不太懂这两者之间的差异,最后通过查找官方文档和一些资料了解到中间的差别。

1、官方文档的解释

官方文档中显示如下:

这里写图片描述

图中翻译如下:

第一段:如果t是一个tf.Tensor对象,则tf.Tensor.eval是tf.Session.run的缩写(其中sess是当前的tf.get_default_session。下面的两个代码片段是等价的:

第二段:在第二个示例中,会话充当上下文管理器,其作用是将其安装为with块的生命周期的默认会话。 上下文管理器方法可以为简单用例(比如单元测试)提供更简洁的代码; 如果您的代码处理多个图形和会话,则可以更直接地对Session.run()进行显式调用。

简单点说就是:你可以使用sess.run()在同一步获取多个tensor中的值,使用Tensor.eval()时只能在同一步当中获取一个tensor值,并且每次使用 eval 和 run时,都会执行整个计算图。

2.Stack Overflow上面的解释

同时我查阅了Stack Overflow上面人们对这个问题的解释:可以贴在下面加深理解。
原问题链接:
http://stackoverflow.com/questions/33610685/in-tensorflow-what-is-the-difference-between-session-run-and-tensor-eval

Question:
TensorFlow has two ways to evaluate part of graph: Session.run on a list of variables and Tensor.eval. Is there a difference between these two?

Answer:
If you have a Tensor t, calling t.eval() is equivalent to calling tf.get_default_session().run(t).
You can make a session the default as follows:

t = tf.constant(42.0)
sess = tf.Session()
with sess.as_default():   # or `with sess:` to close on exit
    assert sess is tf.get_default_session()
    assert t.eval() == sess.run(t)
    
    
  • 1
  • 2
  • 3
  • 4
  • 5

The most important difference is that you can use sess.run() to fetch the values of many tensors in the same step:

t = tf.constant(42.0)
u = tf.constant(37.0)
tu = tf.mul(t, u)
ut = tf.mul(u, t)
with sess.as_default():
   tu.eval()  # runs one step
   ut.eval()  # runs one step
   sess.run([tu, ut])  # evaluates both tensors in a single step
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Note that each call to eval and run will execute the whole graph from scratch. To cache the result of a computation, assign it to a tf.Variable.

翻译如下:

问题:
tensorflow有两种方式:Session.run和 Tensor.eval,这两者的区别在哪?
答:
如果你有一个Tensor t,在使用t.eval()时,等价于:tf.get_default_session().run(t).
举例:

t = tf.constant(42.0)
sess = tf.Session()
with sess.as_default():   # or `with sess:` to close on exit
    assert sess is tf.get_default_session()
    assert t.eval() == sess.run(t)
    
    
  • 1
  • 2
  • 3
  • 4
  • 5

这其中最主要的区别就在于你可以使用sess.run()在同一步获取多个tensor中的值,
例如:

t = tf.constant(42.0)
u = tf.constant(37.0)
tu = tf.mul(t, u)
ut = tf.mul(u, t)
with sess.as_default():
   tu.eval()  # runs one step
   ut.eval()  # runs one step
   sess.run([tu, ut])  # evaluates both tensors in a single step
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注意到:每次使用 eval 和 run时,都会执行整个计算图,为了获取计算的结果,将它分配给tf.Variable,然后获取。

        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
            </div>

之前在TensorFlow中运行代码时,在会话中会需要运行节点,会碰到两种方式:Session.run()和Tensor.eval(),刚开始不太懂这两者之间的差异,最后通过查找官方文档和一些资料了解到中间的差别。

1、官方文档的解释

官方文档中显示如下:

这里写图片描述

图中翻译如下:

第一段:如果t是一个tf.Tensor对象,则tf.Tensor.eval是tf.Session.run的缩写(其中sess是当前的tf.get_default_session。下面的两个代码片段是等价的:

第二段:在第二个示例中,会话充当上下文管理器,其作用是将其安装为with块的生命周期的默认会话。 上下文管理器方法可以为简单用例(比如单元测试)提供更简洁的代码; 如果您的代码处理多个图形和会话,则可以更直接地对Session.run()进行显式调用。

简单点说就是:你可以使用sess.run()在同一步获取多个tensor中的值,使用Tensor.eval()时只能在同一步当中获取一个tensor值,并且每次使用 eval 和 run时,都会执行整个计算图。

2.Stack Overflow上面的解释

同时我查阅了Stack Overflow上面人们对这个问题的解释:可以贴在下面加深理解。
原问题链接:
http://stackoverflow.com/questions/33610685/in-tensorflow-what-is-the-difference-between-session-run-and-tensor-eval

Question:
TensorFlow has two ways to evaluate part of graph: Session.run on a list of variables and Tensor.eval. Is there a difference between these two?

Answer:
If you have a Tensor t, calling t.eval() is equivalent to calling tf.get_default_session().run(t).
You can make a session the default as follows:

t = tf.constant(42.0)
sess = tf.Session()
with sess.as_default():   # or `with sess:` to close on exit
    assert sess is tf.get_default_session()
    assert t.eval() == sess.run(t)
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

The most important difference is that you can use sess.run() to fetch the values of many tensors in the same step:

t = tf.constant(42.0)
u = tf.constant(37.0)
tu = tf.mul(t, u)
ut = tf.mul(u, t)
with sess.as_default():
   tu.eval()  # runs one step
   ut.eval()  # runs one step
   sess.run([tu, ut])  # evaluates both tensors in a single step
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Note that each call to eval and run will execute the whole graph from scratch. To cache the result of a computation, assign it to a tf.Variable.

翻译如下:

问题:
tensorflow有两种方式:Session.run和 Tensor.eval,这两者的区别在哪?
答:
如果你有一个Tensor t,在使用t.eval()时,等价于:tf.get_default_session().run(t).
举例:

t = tf.constant(42.0)
sess = tf.Session()
with sess.as_default():   # or `with sess:` to close on exit
    assert sess is tf.get_default_session()
    assert t.eval() == sess.run(t)
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

这其中最主要的区别就在于你可以使用sess.run()在同一步获取多个tensor中的值,
例如:

t = tf.constant(42.0)
u = tf.constant(37.0)
tu = tf.mul(t, u)
ut = tf.mul(u, t)
with sess.as_default():
   tu.eval()  # runs one step
   ut.eval()  # runs one step
   sess.run([tu, ut])  # evaluates both tensors in a single step
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注意到:每次使用 eval 和 run时,都会执行整个计算图,为了获取计算的结果,将它分配给tf.Variable,然后获取。

        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
            </div>

猜你喜欢

转载自blog.csdn.net/m0_37192554/article/details/81333447