Keras的多重输出中val_loss的计算
Calculation of val_loss in Keras' multiple output
我有一个关于如何在 Keras 的多重输出中计算 val_loss 的问题。
这是我的代码的摘录。
nBatchSize = 200
nTimeSteps = 1
nInDims = 17
nHiddenDims = 10
nFinalDims = 10
nOutNum = 24
nTraLen = 300
nMaxEP = 20
nValLen = 50
sHisCSV = "history.csv"
oModel = Sequential()
oModel.add(Input(batch_input_shape=(nBatchSize, nTimeSteps, nInDims)))
oModel.add(LSTM(nHiddenDims, return_sequences=True, stateful=True))
oModel.add(LSTM(nHiddenDims, return_sequences=False, stateful=True))
oModel.add(Dense(nFinalDims, activation="relu")
oModel.add(Dense(nOutNum, activation="linear")
oModel.compile(loss="mse", optimizer=Nadam())
oModel.reset_states()
oHis = oModel.fit_generator(oDataGen, steps_per_epoch=nTraLen,
epochs=nMaxEP, shuffle=False,
validation_data=oDataGen, validation_steps=nValLen,
callbacks=[CSVLogger(sHisCSV, append=True)])
# number of cols is nOutNum(=24), number of rows is len(oEvaGen)
oPredDF = pd.DataFrame(oPredModel.predict_generator(oEvaGen, steps=len(oEvaGen))
# GTDF is a dataframe of Ground Truth
nRMSE = np.sqrt(np.nanmean(np.array(np.power(oPredDF - oGTDF, 2))))
在history.csv中写入val_loss,写成3317.36。
预测结果计算出的RMSE为66.4.
据我了解我的 Keras 规范,val_loss 写在 history.csv
是 24 个输出的平均 MSE。
假设它是正确的,RMSE 可以从 history.csv 计算为 11.76 (= sqrt(3317.36/24)),
这与 nRMSE (=66.4) 的值完全不同
正如 sqrt(3317.36) = 57.6 相当接近它。
我对 val_loss 上的 Keras 规范的理解不正确吗?
你的第一个假设是正确的,但是进一步的推导有点错误。
作为 MSE is the mean of the model's output's squared errors, as you can see in the Keras documentation:
mean_squared_error
keras.losses.mean_squared_error(y_true, y_pred)
在 Keras 源代码中:
K.mean(K.square(y_pred - y_true), axis=-1)
因此 RMSE 是此值的平方根:
K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1))
你写的是平方误差的平方根,即RSE。
所以根据你的实际例子:
RSE 可以计算为 sqrt(3317.36/24) = 11.76
RMSE 可以计算为 sqrt(3317.36) = 57.6
因此模型提供的 RMSE(和 nRMSE)值是正确的。
我有一个关于如何在 Keras 的多重输出中计算 val_loss 的问题。 这是我的代码的摘录。
nBatchSize = 200
nTimeSteps = 1
nInDims = 17
nHiddenDims = 10
nFinalDims = 10
nOutNum = 24
nTraLen = 300
nMaxEP = 20
nValLen = 50
sHisCSV = "history.csv"
oModel = Sequential()
oModel.add(Input(batch_input_shape=(nBatchSize, nTimeSteps, nInDims)))
oModel.add(LSTM(nHiddenDims, return_sequences=True, stateful=True))
oModel.add(LSTM(nHiddenDims, return_sequences=False, stateful=True))
oModel.add(Dense(nFinalDims, activation="relu")
oModel.add(Dense(nOutNum, activation="linear")
oModel.compile(loss="mse", optimizer=Nadam())
oModel.reset_states()
oHis = oModel.fit_generator(oDataGen, steps_per_epoch=nTraLen,
epochs=nMaxEP, shuffle=False,
validation_data=oDataGen, validation_steps=nValLen,
callbacks=[CSVLogger(sHisCSV, append=True)])
# number of cols is nOutNum(=24), number of rows is len(oEvaGen)
oPredDF = pd.DataFrame(oPredModel.predict_generator(oEvaGen, steps=len(oEvaGen))
# GTDF is a dataframe of Ground Truth
nRMSE = np.sqrt(np.nanmean(np.array(np.power(oPredDF - oGTDF, 2))))
在history.csv中写入val_loss,写成3317.36。 预测结果计算出的RMSE为66.4.
据我了解我的 Keras 规范,val_loss 写在 history.csv 是 24 个输出的平均 MSE。 假设它是正确的,RMSE 可以从 history.csv 计算为 11.76 (= sqrt(3317.36/24)), 这与 nRMSE (=66.4) 的值完全不同 正如 sqrt(3317.36) = 57.6 相当接近它。
我对 val_loss 上的 Keras 规范的理解不正确吗?
你的第一个假设是正确的,但是进一步的推导有点错误。
作为 MSE is the mean of the model's output's squared errors, as you can see in the Keras documentation:
mean_squared_error
keras.losses.mean_squared_error(y_true, y_pred)
在 Keras 源代码中:
K.mean(K.square(y_pred - y_true), axis=-1)
因此 RMSE 是此值的平方根:
K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1))
你写的是平方误差的平方根,即RSE。
所以根据你的实际例子:
RSE 可以计算为 sqrt(3317.36/24) = 11.76
RMSE 可以计算为 sqrt(3317.36) = 57.6
因此模型提供的 RMSE(和 nRMSE)值是正确的。