如何评估keras调谐器的模型性能。

发布于 2025-01-22 05:33:14 字数 1136 浏览 0 评论 0原文

我目前正在尝试通过在每个时期显示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])

I'm currently trying to visualise the performance of my prediction model by showing the val_mse in every epoch. The code that used to work for model.fit() doesn't work for tuner.search(). Can anyone provide me with some guide on this. Thank you.

Previous code:

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 code:

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])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看春风乍起 2025-01-29 05:33:14

Keras Tuner无法正常工作!

使用张量板回调,还有更多的“ kerastuner” - 友好的方式来可视化训练曲线。

这样定义张板回调。

tensorboard_callback = keras.callbacks.TensorBoard("/content/tb_logs") # The logs will be written to "/content/tb_logs".

修改您的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, tensorboard_callback]
)

用于在Python笔记本(.IPYNB)中运行此操作。

%load_ext tensorboard
%tensorboard --logdir /content/tb_logs

有关更多信息,请访问: https://keras.io/guides/guides/guides/keras_tuner/visal/visalize_tuning/

Keras Tuner does not work that way!

There is more "KerasTuner"-friendly way of visualizing the training curves by using Tensorboard callback.

Define the tensorboard callback like this.

tensorboard_callback = keras.callbacks.TensorBoard("/content/tb_logs") # The logs will be written to "/content/tb_logs".

Modify you tuner.search() method like this.

tuner.search(
    x = normed_train_data,
    y = y_train,
    epochs = 200,
    batch_size=64,
    validation_data=(normed_test_data, y_test),
    callbacks = [early_stopping, tensorboard_callback]
)

For running this in Python Notebook (.ipynb).

%load_ext tensorboard
%tensorboard --logdir /content/tb_logs

For more information visit: https://keras.io/guides/keras_tuner/visualize_tuning/

少女净妖师 2025-01-29 05:33:14

在使用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来训练和评估模型您的数据集并将大大减少损失。

请检查链接作为参考,以获取更多详细信息。

Before using tuner.search to search the best model, you need to install and import keras_tuner:

!pip install keras-tuner --upgrade
import keras_tuner as kt
from tensorflow import keras

Then, define the hyperparameter (hp) in the model definition, for instance as below:

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

Initialize the tuner:

tuner = kt.RandomSearch(build_model,objective='val_loss',max_trials=5)

Now, Start the search and get the best model by using 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]

Hence, Now you can use this best_model to train and evaluate the model with your dataset and will get a significant decrease in loss.

Please check this link as a reference for more detail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文