我試圖弄清楚均方誤差 (MSE) 是如何計算的,tensorflow并且正在閱讀
假設我有一個輸出并創建真實值和預測值。
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,但是,它回傳了 me 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 .
uj5u.com熱心網友回復:
第二個值計算為
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。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/378043.html
