对于研究需要,我想检查tf.Metrics.mean_squared_error的准确性。令我惊讶的是,他们有很大的不同。我正在寻求解释。这是我的经验简介,然后是我的示例代码:
1)用tf.Metrics.mean_squared_error评估训练好的玩具模型和整个训练数据;和
2)在步骤1之后立即再次评估,首先收集给定所有" Xs" (或图像)相同的整个训练数据,然后用训练数据和预测的所有基础事实(或标签)计算均方误差。
我有两个未经证实的解释:(1)浮点精度损失累积和(2)tf.Metrics.mean_square_error在其实现中应用看似移动的平均值,导致不准确。
非常感谢任何相关的想法!谢谢!
import tensorflow as tf
from numpy import genfromtxt
tf.logging.set_verbosity(tf.logging.INFO)
# (hyper)parameters
batch_size = 200
num_epochs = 1000
steps = 1000
# prepare data
with tf.Session() as sess:
training_x = sess.run(tf.random_normal([2048, 16], mean=-1, stddev=4, dtype=tf.float64))
training_y = norm = sess.run(tf.random_normal([2048, 1], mean=-1, stddev=4, dtype=tf.float64))
# input function
_input_fn = lambda _input_path: genfromtxt(_input_path, delimiter=',')
input_training = tf.contrib.learn.io.numpy_input_fn({"input": training_x}, training_y,
batch_size=batch_size, num_epochs=num_epochs)
input_evaluate_train_data = tf.contrib.learn.io.numpy_input_fn({"input": training_x}, training_y)
# remember to give the same column name as used in _input_fn
features = [tf.contrib.layers.real_valued_column('input', dimension=16)]
regressor = tf.contrib.learn.DNNRegressor(feature_columns=features,
hidden_units=[32, 8],
dropout=0.1,
model_dir="testDNNR/result",
optimizer=tf.train.AdamOptimizer(learning_rate=0.008),
activation_fn=tf.nn.elu)
# training
regressor.fit(input_fn=input_training, steps=steps)
# testing with training data
eval_metric_ops = {
"mse": lambda targets, predictions: tf.metrics.mean_squared_error(tf.cast(targets, tf.float64), predictions)
}
ev = regressor.evaluate(input_fn=input_evaluate_train_data, steps=1, metrics=eval_metric_ops)
pred = regressor.predict(input_fn=input_evaluate_train_data, as_iterable=False)
# using my MSE
mse = ((training_y - pred) ** 2).mean()
print ("evaluation result given training data using my MSE: " + str(mse))
print ("evaluation result given training data using the library built-in MSE: " + str(ev))
答案 0 :(得分:0)
不匹配来自tf.contrib.learn.io.numpy_input_fn的工作原理。经过一些包装后,此功能由https://github.com/tensorflow/tensorflow/blob/r1.3/tensorflow/python/estimator/inputs/numpy_io.py#L45
实现正如您所看到的,默认情况下,每次调用它都会返回一批128个值。当您在致电regressor.evaluate
时使用它时,您获得的128个值的值与您在regressor.predict
中使用时所获得的值不同。
还有其他相关问题。上面链接的实现有shuffle=True
,这意味着它将从您的数据中随机选择128个元素。
此外,当你training_y - pred
时,尺寸会有很大不同,张量会被广播,从而产生的条款会超出预期。