为什么将某些指标添加到模型后损失图会消失?

Why do loss graphs disappear once some metrics are added to a model?

在评估为下面的回归问题合成的训练模型的过程中,我在绘制结果 history 时有些困惑。特别是,当我不考虑任何 metrics

import pandas as pd
import matplotlib.pyplot as plt

import tensorflow as tf

from sklearn.datasets import fetch_california_housing
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split

housing = fetch_california_housing()

X_train_full, X_test, y_train_full, y_test = train_test_split(
    housing.data, housing.target)

X_train, X_valid, y_train, y_valid = train_test_split(
    X_train_full, y_train_full)

scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)
X_valid = scaler.fit_transform(X_valid)
X_test = scaler.fit_transform(X_test)

model = tf.keras.Sequential([
    tf.keras.layers.Dense(30, tf.keras.activations.relu, input_shape=X_train.shape[1:]),
    tf.keras.layers.Dense(1)
])

model.compile(loss=tf.keras.losses.mean_squared_error,
              optimizer=tf.keras.optimizers.SGD())

history = model.fit(X_train, y_train, epochs=20,
                    validation_data=(X_valid, y_valid))

pd.DataFrame(history.history).plot()
plt.grid(True)
plt.show()

最终图包括预期的 lossval_loss 个图表。

但是一旦我将 metrics 添加到我的模型中,比如 tf.keras.metrics.MeanSquaredError()

生成的结果图
import pandas as pd
import matplotlib.pyplot as plt

import tensorflow as tf

from sklearn.datasets import fetch_california_housing
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split

housing = fetch_california_housing()

X_train_full, X_test, y_train_full, y_test = train_test_split(
    housing.data, housing.target)

X_train, X_valid, y_train, y_valid = train_test_split(
    X_train_full, y_train_full)

scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)
X_valid = scaler.fit_transform(X_valid)
X_test = scaler.fit_transform(X_test)

model = tf.keras.Sequential([
    tf.keras.layers.Dense(30, tf.keras.activations.relu, input_shape=X_train.shape[1:]),
    tf.keras.layers.Dense(1)
])

model.compile(loss=tf.keras.losses.mean_squared_error,
              optimizer=tf.keras.optimizers.SGD(),
              metrics=[tf.keras.metrics.MeanSquaredError()])

history = model.fit(X_train, y_train, epochs=20,
                    validation_data=(X_valid, y_valid))

pd.DataFrame(history.history).plot()
plt.grid(True)
plt.show()

缺少 lossval_loss 个草图。

这里有什么问题?

编辑:

这里是history.history的内容:

{'loss': [0.880902886390686, 0.6208109855651855, 0.5102624297142029, 0.47074252367019653, 0.4556053578853607, 0.4464321732521057, 0.44210636615753174, 0.43378400802612305, 0.42544370889663696, 0.428415447473526], 'mean_squared_error': [0.880902886390686, 0.6208109855651855, 0.5102624297142029, 0.47074252367019653, 0.4556053578853607, 0.4464321732521057, 0.44210636615753174, 0.43378400802612305, 0.42544370889663696, 0.428415447473526], 'val_loss': [0.6332216262817383, 0.514700710773468, 0.4509757459163666, 0.46695834398269653, 0.5228265523910522, 0.6748611330986023, 0.6648175716400146, 0.7329052090644836, 0.8352308869361877, 1.081600546836853], 'val_mean_squared_error': [0.6332216262817383, 0.514700710773468, 0.4509757459163666, 0.46695834398269653, 0.5228265523910522, 0.6748611330986023, 0.6648175716400146, 0.7329052090644836, 0.8352308869361877, 1.081600546836853]}

你的loss是均方误差,你的metric是均方误差,完全一样。这意味着当你绘制它们时它们是重叠的!