具有缩放目标值的 Keras 回归器 MSE

问题描述

在缩放目标值时,我无法解释 MSE。使用 MinMaxScaler,缩放目标值。

#scale all data
scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train_pre)
X_test = scaler.transform(X_test_pre)

yscaler = MinMaxScaler()
y_train = yscaler.fit_transform( y_train_pre )
y_test = yscaler.transform( y_test_pre )

当我使用模型评估测试数据时,损失接近模型的最后一次 EPOCH 损失。然后我使用 predict 来获得测试损失,并且一切正常(至少是一致的)。

#evaluate Test set
mse_test = model.evaluate(X_test,y_test)  #returns scaled loss of .0325 (close to last epoch LOSS)

#evaluate performance of test is to predict the results and compare to the actuals
y_pred = model.predict(X_test)
actual = y_test

#note we use the same LOSS metric here as we did when the model was compiled
test_loss = np.mean(keras.losses.mean_squared_error(y_pred,actual))
test_loss
#scaled loss of .0325 returned - matches what evaluate() provided

最后,为了获得实际损失,我对缩放损失值使用 inverse_transform 并获得预期损失。

yscaler.inverse_transform( np.array(test_loss).reshape(-1,1) ) #returns actual loss of .307 which is what is expected

我似乎无法理解并“工作”的是,我的预测(我认为)的 inverse_transform 应该给我我的 ACTUAL 损失(未缩放)并且与上述类似,但事实并非如此。

>
#try to get same loss
predictions = yscaler.inverse_transform( model.predict(X_test) )
actuals = yscaler.inverse_transform( y_test )
np.mean(keras.losses.mean_squared_error(predictions,actuals)) #loss is reported as .764
#WHY isn't the MSE close to the loss of .307?

#try taking the square root and still no luck - loss is .679
np.mean(np.sqrt(keras.losses.mean_squared_error(predictions,actuals)))

**使用 SKEARN Housing 数据集完成下面的代码段 **

import numpy as np
import tensorflow as tf
from tensorflow import keras
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler,MinMaxScaler

from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()

X_train_pre,X_test_pre,y_train_pre,y_test_pre = train_test_split(housing.data,housing.target,random_state=42)

#shape of our y data sets
y_train_pre = y_train_pre.reshape(-1,1)
y_test_pre = y_test_pre.reshape(-1,1)

#scale all data
scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train_pre)
X_test = scaler.transform(X_test_pre)    
yscaler = MinMaxScaler()
y_train = yscaler.fit_transform( y_train_pre )
y_test = yscaler.transform( y_test_pre )    

np.random.seed(42)
tf.random.set_seed(42)
model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=X_train.shape[1:]),keras.layers.Dense(30,activation="relu"),keras.layers.Dense(1)
])
model.compile(loss="mean_squared_error",optimizer=keras.optimizers.SGD(lr=1e-3))
history = model.fit(X_train,y_train,epochs=10)

#evaluate Test set
mse_test = model.evaluate(X_test,y_test)  #returns scaled loss of .0325 (close to last epoch LOSS)


#evaluate performance of test is to predict the results and compare to the actuals
y_pred = model.predict(X_test)
actual = y_test

#note we use the same LOSS metric here as we did when the model was compiled
test_loss = np.mean(keras.losses.mean_squared_error(y_pred,actual))
test_loss
#scaled loss of .0325 returned - matches what evaluate() provided


yscaler.inverse_transform( np.array(test_loss).reshape(-1,1) ) 
#returns actual loss of .307 which is what is expected


#try to get same loss
predictions = yscaler.inverse_transform( model.predict(X_test) )
actuals = yscaler.inverse_transform( y_test )
np.mean(keras.losses.mean_squared_error(predictions,actuals))
#loss is reported as .764
#WHY isn't the MSE close to the loss of .307?


#try taking the square root and still no luck - loss is .679
np.mean(np.sqrt(keras.losses.mean_squared_error(predictions,actuals)))

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)