| |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
-> 人工智能 -> Introduction to the Keras Tuner -> 正文阅读 |
|
[人工智能]Introduction to the Keras Tuner |
OverviewThe Keras Tuner is a library that helps you pick the optimal set of hyperparameters for your TensorFlow program. The process of selecting the right set of hyperparameters for your machine learning (ML) application is called hyperparameter tuning or hypertuning. Hyperparameters are the variables that govern the training process and the topology of an ML model. These variables remain constant over the training process and directly impact the performance of your ML program. Hyperparameters are of two types:
In this tutorial, you will use the Keras Tuner to perform hypertuning for an image classification application. Setupimport tensorflow as tf from tensorflow import keras import keras_tuner as kt Download and prepare the datasetIn this tutorial, you will use the Keras Tuner to find the best hyperparameters for a machine learning model that classifies images of clothing from the Fashion MNIST dataset. Load the data. (img_train, label_train), (img_test, label_test) = keras.datasets.fashion_mnist.load_data() # Normalize pixel values between 0 and 1 img_train = img_train.astype('float32') / 255.0 img_test = img_test.astype('float32') / 255.0 Define the modelWhen you build a model for hypertuning, you also define the hyperparameter search space in addition to the model architecture. The model you set up for hypertuning is called a hypermodel. You can define a hypermodel through two approaches:
You can also use two pre-defined In this tutorial, you use a model builder function to define the image classification model. The model builder function returns a compiled model and uses hyperparameters you define inline to hypertune the model. def model_builder(hp): model = keras.Sequential() model.add(keras.layers.Flatten(input_shape=(28, 28))) ? # Tune the number of units in the first Dense layer # Choose an optimal value between 32-512 hp_units = hp.Int('units', min_value=32, max_value=512, step=32) model.add(keras.layers.Dense(units=hp_units, activation='relu')) model.add(keras.layers.Dense(10)) ? # Tune the learning rate for the optimizer # Choose an optimal value from 0.01, 0.001, or 0.0001 hp_learning_rate = hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4]) ? model.compile(optimizer=keras.optimizers.Adam(learning_rate=hp_learning_rate), ? ? ? ? ? ? ? loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), ? ? ? ? ? ? ? metrics=['accuracy']) ? return model Instantiate the tuner and perform hypertuningInstantiate the tuner to perform the hypertuning. The Keras Tuner has four tuners available - To instantiate the Hyperband tuner, you must specify the hypermodel, the tuner = kt.Hyperband(model_builder, ? ? ? ? ? ? ? ? ? ? objective='val_accuracy', ? ? ? ? ? ? ? ? ? ? max_epochs=10, ? ? ? ? ? ? ? ? ? ? factor=3, ? ? ? ? ? ? ? ? ? ? directory='my_dir', ? ? ? ? ? ? ? ? ? ? project_name='intro_to_kt') The Hyperband tuning algorithm uses adaptive resource allocation and early-stopping to quickly converge on a high-performing model. This is done using a sports championship style bracket. The algorithm trains a large number of models for a few epochs and carries forward only the top-performing half of models to the next round. Hyperband determines the number of models to train in a bracket by computing 1 + log Create a callback to stop training early after reaching a certain value for the validation loss. stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5) Run the hyperparameter search. The arguments for the search method are the same as those used for tuner.search(img_train, label_train, epochs=50, validation_split=0.2, callbacks=[stop_early]) ? # Get the optimal hyperparameters best_hps=tuner.get_best_hyperparameters(num_trials=1)[0] ? print(f""" The hyperparameter search is complete. The optimal number of units in the first densely-connected layer is {best_hps.get('units')} and the optimal learning rate for the optimizer is {best_hps.get('learning_rate')}. """) Train the modelFind the optimal number of epochs to train the model with the hyperparameters obtained from the search. # Build the model with the optimal hyperparameters and train it on the data for 50 epochs model = tuner.hypermodel.build(best_hps) history = model.fit(img_train, label_train, epochs=50, validation_split=0.2) ? val_acc_per_epoch = history.history['val_accuracy'] best_epoch = val_acc_per_epoch.index(max(val_acc_per_epoch)) + 1 print('Best epoch: %d' % (best_epoch,)) Re-instantiate the hypermodel and train it with the optimal number of epochs from above. hypermodel = tuner.hypermodel.build(best_hps) ? # Retrain the model hypermodel.fit(img_train, label_train, epochs=best_epoch, validation_split=0.2) To finish this tutorial, evaluate the hypermodel on the test data. eval_result = hypermodel.evaluate(img_test, label_test) print("[test loss, test accuracy]:", eval_result) The |
|
|
上一篇文章 下一篇文章 查看所有文章 |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 | -2024/11/17 20:35:11- |
|
网站联系: qq:121756557 email:121756557@qq.com IT数码 |