我目前正在学习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实现中得到了不同的答案,cost得到了4.5,而cost2得到了9。根据均方误差的公式,我应该得到一个值为9。但是第一个代价函数是我正在尝试学习的Python笔记本中实现的那个。