目前,我正试图通过在每个时代展示val_mse来可视化我的预测模型的性能。用于model.fit()的代码不适用于tuner.search()。有人能为我提供一些关于这方面的指导吗。谢谢。
以前的代码:
import matplotlib.pyplot as plt
def plot_model(history):
hist = pd.DataFrame (history.history)
hist['epoch'] = history.epoch
plt.figure()
plt.xlabel('Epoch')
plt.ylabel('Mean Absolute Error')
plt.plot(hist['epoch'], hist['mae'],
label='Train Error')
plt.plot(hist['epoch'], hist['val_mae'],
label = 'Val Error')
plt.legend()
plt.ylim([0,20])
plt.figure()
plt.xlabel('Epoch')
plt.ylabel('Mean Square Error')
plt.plot (hist['epoch'], hist['mse'],
label='Train Error')
plt.plot (hist['epoch'], hist['val_mse'],
label = 'Val Error')
plt.legend()
plt.ylim([0,400])
plot_model(history)keras.tuner代码:
history = tuner.search(x = normed_train_data,
y = y_train,
epochs = 200,
batch_size=64,
validation_data=(normed_test_data, y_test),
callbacks = [early_stopping])发布于 2022-04-20 12:50:54
在使用tuner.search搜索最佳模型之前,需要安装和导入keras_tuner
!pip install keras-tuner --upgrade
import keras_tuner as kt
from tensorflow import keras然后,在模型定义中定义超参数(hp),例如如下所示:
def build_model(hp):
model = keras.Sequential()
model.add(keras.layers.Dense(
hp.Choice('units', [8, 16, 32]), # define the hyperparameter
activation='relu'))
model.add(keras.layers.Dense(1, activation='relu'))
model.compile(loss='mse')
return model初始化调谐器:
tuner = kt.RandomSearch(build_model,objective='val_loss',max_trials=5)现在,开始搜索并使用tuner.search获得最佳模型
tuner.search(x = normed_train_data,
y = y_train,
epochs = 200,
batch_size=64,
validation_data=(normed_test_data, y_test),
callbacks = [early_stopping])
best_model = tuner.get_best_models()[0]因此,现在您可以使用这个best_model来使用您的数据集来训练和评估模型,并且损失将大大减少。
有关更多细节,请查看这链接作为参考。
https://stackoverflow.com/questions/71914744
复制相似问题