tensorflow tf.metrics.mean_absolute_error返回不同的值吗?

时间:2019-05-17 14:00:41

标签: tensorflow deep-learning

我正在测试tf.metrics.mean_absolute_error功能。我知道整个会话返回两个结果。但是,我发现当我运行程序时,它不会为相同的输入返回值。我的示例代码如下:

import tensorflow as tf
import numpy as np

pred_0 = np.zeros(5) + 1
pred_1 = np.zeros(5) + 2
pred_2 = np.zeros(5) + 3
pred_3 = np.zeros(5) + 4
pred_4 = np.zeros(5) + 5

label = np.asarray([0, 0, 0, 0, 0])
predictions = np.stack((pred_0, pred_1, pred_2, pred_3, pred_4))

print('____________predictions______________')
print(predictions)

print('____________labels______________')
print(label)

pred_placeholder = tf.placeholder(tf.float16, shape=label.shape)
label_placeholder = tf.placeholder(tf.float16, label.shape)
mae = tf.metrics.mean_absolute_error(label_placeholder, pred_placeholder)

init = tf.group(tf.global_variables_initializer(), 
tf.local_variables_initializer())

with tf.Session() as sess:
    sess.run(init)
    for i in range(predictions.shape[0]):
        mae_value, update_op = sess.run(mae, feed_dict={label_placeholder: label, pred_placeholder: predictions[i]})
        print(f'{mae_value} {update_op} {np.mean(predictions[i] - label)}')

但是,当我多次运行此代码片段时,它会打印出不同的值! 有时,代码的输出为:

0.0 1.0 1.0
1.0 1.5 2.0
1.5 2.0 3.0
2.0 2.5 4.0
3.0 3.0 5.0

有时是:

1.0 1.0 1.0
3.0 1.5 2.0
3.0 2.0 3.0
3.3333332538604736 2.5 4.0
3.75 3.0 5.0

或者可能是

0.0 1.0 1.0
3.0 1.5 2.0
3.0 2.0 3.0
3.3333332538604736 2.5 4.0
3.75 3.0 5.0

我对此输出感到很困惑。

0 个答案:

没有答案