我目前正在学习 TensorFlow,并遇到了这个 notebook。
我对如何实现均方误差成本函数有疑问:
import tensorflow as tf
import numpy as np
predicted = np.array([1,2,3])
Y = np.array([4,5,6])
num_instances = predicted.shape[0]
cost = tf.reduce_sum(tf.pow(predicted-Y, 2))/(2*num_instances)
cost2 = tf.reduce_mean(tf.square(predicted - Y))
with tf.Session() as sess:
print(sess.run(cost))
print(sess.run(cost2))
我不明白为什么它必须将第一个成本函数的分母乘以 2。我从 MSE 的不同实现中得到了不同的答案,成本产生 4.5 而成本 2 产生 9。按照均方误差的公式,我应该得到一个 9 的值。但是第一个成本函数是在我试图学习的 python 笔记本中实现的。