当我要求tensorflow计算时会发生什么

时间:2017-09-03 00:57:59

标签: tensorflow

考虑这个非常简单的例子:

/// use 'ref return' to initialize a new 'ref local' tuple 'aa'
ref (int, int) aa = ref GetKnownTuple();

/// or use the same function without 'ref' to create a local COPY 'bb'
var bb = GetKnownTuple();

/// use 'ref' parameter to modify values of local copy 'bb' ('aa/g' are not altered)
SetValues(3, 4, ref bb);

/// deconstruction of 'ref local' tuple; reads values from referent 'g' (1, 2)
(int x, int y) = aa;

/// 'ref local' reference to a local tuple copy
ref (int, int) dd = ref bb;

/// use 'out' parameter to construct a new (non-'ref') local tuple 'cc'
SetValuesOut(y, x, out (int, int) cc);

/// ...or use 'out' with 'ref local' to wholly replace existing referent ('g' here)
SetValuesOut(5, 6, out aa);

/// 'ref return' function can also be used as assignment l-value...
GetKnownTuple() = (7, 8);

/// ('aa/g' are altered; locals 'bb' and 'cc' remain unchanged)

/// ...or assign a referent via 'ref local' variable (changes 'g' again)
aa = (9, 10);

/// conditional assignment via 'ref return'  (changes 'g' again)
SelectRef(0, ref aa, ref bb, ref cc) = (11, 12);

tensorflow只会计算a = tf.placeholder(tf.float32) b = a + 2 c = b + 4 sess = tf.InteractiveSession() tf.global_variables_initializer().run() sess.run([b, c], feed_dict={a: 1}) 一次,或两次吗?

3 个答案:

答案 0 :(得分:0)

它将计算1次。

TensorFlow运行图表定义的计算并获取值[b,c]以进行输出。

答案 1 :(得分:0)

只有一次(但我想你知道)

TensorBoard

答案 2 :(得分:0)

b 只会计算一次。结果将用于计算 c

这里有一个来自Tensorflow programmer's guide

的简短示例
x = tf.constant([[37.0, -23.0], [1.0, 4.0]])
w = tf.Variable(tf.random_uniform([2, 2]))
y = tf.matmul(x, w)
output = tf.nn.softmax(y)
init_op = w.initializer

with tf.Session() as sess:
  # Run the initializer on `w`.
  sess.run(init_op)

  # Evaluate `output`. `sess.run(output)` will return a NumPy array containing
  # the result of the computation.
  print(sess.run(output))

  # Evaluate `y` and `output`. Note that `y` will only be computed once, and its
  # result used both to return `y_val` and as an input to the `tf.nn.softmax()`
  # op. Both `y_val` and `output_val` will be NumPy arrays.
  y_val, output_val = sess.run([y, output])