首页 > 解决方案 > 为什么我的损失价值如此巨大?在 TensorFlow 上运行 MLP

问题描述

我正在运行一个普通的 Sequential API MLP,并且在我的输出中遇到了巨大的损失。我不确定出了什么问题以及如何解决。我正在使用批量标准化层,因为我读到这会有所帮助,而且确实如此,但我的输出仍然很荒谬。任何其他关于架构的建议都非常受欢迎。

RMSE损失约为56000

from tensorflow.keras.models import Sequential

model = Sequential()
model.add(Dense(3, input_dim=X_train_enc.shape[1], activation='relu', kernel_initializer='he_normal'))
model.add(BatchNormalization())
model.add(Dense(10, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(20, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(30, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(20, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(10, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(3, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(1, activation = 'linear'))

# # summarize layers
print(model.summary())

from tensorflow.keras import backend
from tensorflow.keras.losses import mean_squared_error

#Creating/Defining our own metrics
def mean_absolute_percentage_error(y_true, y_pred):
    diff = K.abs((y_true - y_pred) / K.clip(K.abs(y_true),
                                        K.epsilon(),
                                        None))
    return 100. * K.mean(diff, axis=-1)
tf.keras.backend.set_epsilon(1) #https://stackoverflow.com/questions/49729522/why-is-the-mean-average-percentage-errormape-extremely-high


def rmse(y_true, y_pred):
    return backend.sqrt(mean_squared_error(y_true, y_pred))

#def rmse(y_true, y_pred):
#    return backend.sqrt(backend.mean(backend.square(y_pred - y_true), axis=-1))

Adamax = tf.keras.optimizers.Adamax(lr=0.02, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)

#Compiling model
model.compile(optimizer='adam',loss=rmse, metrics=['accuracy',rmse, 'mae', 'mape'])#Metric functions are similar to loss functions, except that the results from evaluating a metric are not used when training the model

# # Creating a checkpoint
filepath="weights-improvement-{epoch:02d}-{val_rmse:.9f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_rmse', verbose=1, save_best_only=True, mode='min')
# # monitor = "val_loss", save_best_only=False
callbacks_list = [checkpoint]

history = model.fit(X_train_enc, y_train, epochs=200, batch_size=16, validation_split=0.2681867, callbacks=callbacks_list, verbose=1)


标签: pythontensorflowkerasloss

解决方案


推荐阅读