[英]Mean squared error computed by TensorFlow
我試圖弄清楚tensorflow
是如何計算均方誤差 (MSE) 的,並且正在閱讀 https 上的帖子://www.Z2C39BC19B761AC36DC046245D1_D47FE6Z.org/api_docs/squaredpython/metrics
首先,MSE 定義為(參見https://en.wikipedia.org/wiki/Mean_squared_error ):
假設我有一個 output 並創建真實值和預測值。
import numpy as np
import random
y_true = np.random.randint(0, 10, size=(2, 1))
print(y_true,"\n")
y_pred = np.random.randint(0,5,size=(2, 1))
print(y_pred)
[[7]
[5]]
[[2]
[2]]
當我調用tf.keras.losses.mean_squared_error(y_true, y_pred)
時,我期望看到的是[(7-2)^2 + (5-2)^2]/2 = 17
,但是,它返回給我array([25, 9])
。 為什么 tensorflow 不計算平均值?
然后,我增加列號。
y_true = np.random.randint(0, 10, size=(2, 3))
print(y_true,"\n")
y_pred = np.random.randint(0,5,size=(2, 3))
print(y_pred)
[[2 6 0]
[3 3 4]]
[[4 2 4]
[3 4 2]]
tensorflow
返回的答案是array([12, 1])
。 我無法理解這些值是如何計算的。 我所期待的是[(2-4)^2+ (6-2)^2+(0-4)^2]/2 + [(3-3)^2 + (3-4)^2+ (4-2)^2]/2
。
第二個值計算為
a = np.array([2,6,0])
b = np.array([4,2,4])
((b - a)**2).mean()
或[(2-4)^2+ (6-2)^2+(0-4)^2]/3
根據他們的文檔,它相當於np.mean(np.square(y_true - y_pred), axis=-1)
所以它正在逐行計算mse。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.