tf.metrics.mean返回值含义解析:为何两次求值结果不同?
Understanding TensorFlow's
tf.metrics.mean Behavior Let's break down exactly what's happening with your code and why you're seeing those seemingly inconsistent results.
Core Background: What tf.metrics.mean Returns
When you call tf.metrics.mean(test_tensor), it returns a tuple of two distinct TensorFlow objects:
mean(first element): A tensor representing the cumulative average of all values processed so far. This value relies on two internal local variables: a running total of all values, and a count of how many values have been added. Every time you run theupdate_op, these variables get updated.update_op(second element): An operation that does two key things:- Calculates the average of the current input tensor (in your case, this is always 3.5 since your input is a constant tensor).
- Adds the sum of the input tensor to the running total, and increments the count by the number of elements in the input.
- Finally, it returns the average of the current input (not the cumulative average).
Why Your Output Looks the Way It Does
Let's walk through each step of your execution to clarify:
First
sess.run(test_mean):- TensorFlow reads the
meantensor first (beforeupdate_opruns). Initially, the running total and count are both 0, someanreturns 0.0. - Then
update_opexecutes: it calculates the input's average (3.5), updates the running total to 21 (sum of your 6 elements) and count to 6. It returns the input's average (3.5). - Result:
(0.0, 3.5)
- TensorFlow reads the
Second to fourth
sess.run(test_mean):- Each time you run the tuple, TensorFlow reads the current cumulative
meanbefore executingupdate_op. Themeanvalue you see is the cumulative average from all previous updates, not including the current one. This is why it gradually approaches 3.5: eachupdate_opadds another full batch of values to the running state, and you're seeing the state before that latest addition. - Results:
(1.75, 3.5),(2.3333333, 3.5),(2.625, 3.5)
- Each time you run the tuple, TensorFlow reads the current cumulative
sess.run(test_mean[0]):- By this point, you've run
update_opfour times. The cumulative total and count have been fully updated to reflect all four batches, so the cumulative average is exactly 3.5. When you readmeandirectly, you're getting the latest, fully updated cumulative value. - Result:
3.5
- By this point, you've run
sess.run(test_mean[1]):- The
update_opalways recalculates the average of your fixed input tensor, so it will always return 3.5, no matter how many times you run it. - Result:
3.5
- The
Key Takeaway
- Running the full tuple
test_meangives you the old cumulative state before updating it, plus the current input's average. - Running
test_mean[0]alone reads the latest, fully updated cumulative average. test_mean[1]always returns the average of the current input, independent of the cumulative state.
内容的提问来源于stack exchange,提问作者quant
相关产品推荐
相关产品推荐

