0

缩放目标值时,我无法解释我的 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

最后,为了获得 ACTUAL 损失,我在缩放损失值上使用了 inverse_transform,我得到了预期的损失。

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

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

#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)))

**下面使用 SKLEARN 住房数据集的完整代码段**

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)))
4

0 回答 0