python - 为什么我的损失价值如此巨大?在 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)
解决方案
推荐阅读
- java - 循环难度
- jupyter-notebook - 我们如何将 Unicode 值数组转换回 Julia 中的文本形式?
- spring - 缓存 Spring 云网关响应
- pandas - 我的熊猫代码不工作,在教程中相同的代码工作没有任何错误
- c# - 使用 LINQ,其中一个对象属性的值为 x,我如何在不同的属性中返回值
- javascript - Font Awesome 不再适用于浏览器扩展
- android - 电容器离子vuejs android应用程序无法在真实设备中运行
- python - 我正在尝试将图片导入 pygame 但它没有加载
- html - CSS文件不影响Django中的页面
- javascript - ionic,firebase:如何从 firebase 身份验证中获取所有用户电子邮件