sess.run()和".eval()";在张量流编程中 [英] sess.run() and ".eval()" in tensorflow programming

查看:51
本文介绍了sess.run()和".eval()";在张量流编程中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Tensorflow编程中,有人可以告诉我".eval()"和"sess.run()"之间的区别是什么.他们每个人都干什么以及何时使用它们?

In Tensorflow programming, can someone please tell what is the difference between ".eval()" and "sess.run()". What do each of them do and when to use them?

推荐答案

session 对象封装了评估Tensor对象的环境.

A session object encapsulates the environment in which Tensor objects are evaluated.

如果 x tf.Tensor 对象,则 tf.Tensor.eval tf.Session.run <的简写/code>,其中 sess 是当前的 tf.get_default_session .

If x is a tf.Tensor object, tf.Tensor.eval is shorthand for tf.Session.run, where sess is the current tf.get_default_session.

您可以将会话设置为以下默认值

You can make session the default as below

x = tf.constant(5.0)
y = tf.constant(6.0)
z = x * y

with tf.Session() as sess:
  print(sess.run(z))   # 30.0
  print(z.eval())      # 30.0

最重要的区别是您可以使用 sess.run 在以下相同的步骤中获取许多张量的值

The most important difference is you can use sess.run to fetch the values of many tensors in the same step as below

print(sess.run([x,y])) # [5.0, 6.0]
print(sess.run(z))     # 30.0

eval 一次按如下所示获取单个张量值

Where as eval fetch single tensor value at a time as below

print(x.eval()) # 5.0
print(z.eval()) # 3.0

TensorFlow计算定义了一个计算图,该计算图在进行如下评估之前没有数值

TensorFlow computations define a computation graph that has no numerical value until evaluated as below

print(x) # Tensor("Const_1:0", shape=(), dtype=float32)

Tensorflow 2.x(> = 2.0)中,您可以使用 tf.compat.v1.Session()代替 tf.session()

In Tensorflow 2.x (>= 2.0), You can use tf.compat.v1.Session() instead of tf.session()

这篇关于sess.run()和".eval()";在张量流编程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆