【发布时间】:2021-05-06 11:16:30
【问题描述】:
在评估为下面的回归问题合成的训练模型的过程中,我在绘制结果 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()
最终的情节包括 loss 和 val_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()
缺少loss 和val_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]}
【问题讨论】:
-
你能打印history.history里面的内容吗?
-
@SashimiDélicieux:请检查编辑。
标签: python keras tensorflow2.0 tf.keras