IT

TensorFlow에서 Session.run ()과 Tensor.eval ()의 차이점은 무엇입니까?

lottoking 2020. 5. 14. 08:19
반응형

TensorFlow에서 Session.run ()과 Tensor.eval ()의 차이점은 무엇입니까?


TensorFlow에는 두 가지 방법으로 그래프의 일부를 평가할 수 있습니다 : Session.run변수 목록 및 Tensor.eval. 이 둘 사이에 차이점이 있습니까?


Tensort 가있는 경우 calling t.eval()calling 와 같습니다 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)

가장 중요한 차이점은 sess.run()동일한 단계에서 많은 텐서의 값을 가져 오는 데 사용할 수 있다는 것입니다 .

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

호출 할 때마다 참고 eval하고 run처음부터 전체 그래프를 실행합니다. 계산 결과를 캐시하려면에 할당하십시오 tf.Variable.


텐서 흐름에 대한 FAQ 세션에는 정확히 같은 질문에 대한 답변이 있습니다. 계속해서 여기에 남겨 두겠습니다.


경우 tA는 Tensor객체 t.eval()에 대한 속기 sess.run(t)경우 ( sess현재 기본 세션은 코드의 다음 두 조각이 동일합니다. :

sess = tf.Session()
c = tf.constant(5.0)
print sess.run(c)

c = tf.constant(5.0)
with tf.Session():
  print c.eval()

두 번째 예에서 세션은 컨텍스트 관리자 역할을하며 이는 with블록 수명 동안 기본 세션으로 세션을 설치하는 효과가 있습니다. 컨텍스트 관리자 접근 방식은 간단한 사용 사례 (예 : 단위 테스트)를위한보다 간결한 코드로 이어질 수 있습니다. 코드에서 여러 그래프와 세션을 처리하는 경우을 명시 적으로 호출하는 것이 더 간단 할 수 있습니다 Session.run().

많은 것을 분명히 할 수 있으므로 FAQ 전체에서 최소한 탈지하는 것이 좋습니다.


eval() 리스트 객체를 처리 할 수 ​​없습니다

tf.reset_default_graph()

a = tf.Variable(0.2, name="a")
b = tf.Variable(0.3, name="b")
z = tf.constant(0.0, name="z0")
for i in range(100):
    z = a * tf.cos(z + i) + z * tf.sin(b - i)
grad = tf.gradients(z, [a, b])

init = tf.global_variables_initializer()

with tf.Session() as sess:
    init.run()
    print("z:", z.eval())
    print("grad", grad.eval())

하지만 Session.run()할 수 있습니다

print("grad", sess.run(grad))

내가 틀렸다면 나를 바로 잡아 줘


In tensorflow you create graphs and pass values to that graph. Graph does all the hardwork and generate the output based on the configuration that you have made in the graph. Now When you pass values to the graph then first you need to create a tensorflow session.

tf.Session()

Once session is initialized then you are supposed to use that session because all the variables and settings are now part of the session. So, there are two ways to pass external values to the graph so that graph accepts them. One is to call the .run() while you are using the session being executed.

Other way which is basically a shortcut to this is to use .eval(). I said shortcut because the full form of .eval() is

tf.get_default_session().run(values)

You can check that yourself. At the place of values.eval() run tf.get_default_session().run(values). You must get the same behavior.

what eval is doing is using the default session and then executing run().


The most important thing to remember:

The only way to get a constant, variable (any result) from TenorFlow is the session.

Knowing this everything else is easy:

Both tf.Session.run() and tf.Tensor.eval() get results from the session where tf.Tensor.eval() is a shortcut for calling tf.get_default_session().run(t)


I would also outline the method tf.Operation.run() as in here:

After the graph has been launched in a session, an Operation can be executed by passing it to tf.Session.run(). op.run() is a shortcut for calling tf.get_default_session().run(op).

참고URL : https://stackoverflow.com/questions/33610685/in-tensorflow-what-is-the-difference-between-session-run-and-tensor-eval

반응형