TF之eval简述

TF学习中,经常看到tensor.eval这样的用法。
tensor.eval()的意义和sess.run()一样,t.eval()等效于sess.run(t).
但是二者也有些微区别,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

t.eval() 运行必须包含在默认的sess之内,比如:
with sess:
或者:
with tf.Session() as sess:
with sess.as_default():
再或者使用InteractiveSession代替Session,这样子不用放在with之内了

你可能感兴趣的:(神经网络学习)