How to optimize the Hyperparameter of Neural Network Model in Keras
Today, I will talk to you about how to tune the neural network model hyperparameters in Keras, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
In the current research of neural network super-parameter adjustment, tensorflow/keras community has developed an automatic parameter adjustment tool keras-tuner. Through keras-tuner, when we make tensorflow/keras engineering practice, we can easily help us to do some work in the part of model parameter optimization.
First, let's install keras-tuner
Request:
Python 3.6
TensorFlow 2.0
Install from pypi
Pip install-U keras-tuner
It's easy to use. Import first.
Import kerastuner as kt
First, let's introduce the parameter class HyperParameters of keras-tuner, which is very important.
Hp = kt.HyperParameters ()
The HyperParameters class acts as a hyerparameter container. An instance of HyperParameters contains information about the search space and the current value of each superparameter. Of course, you can also define hyperparameters inline with model-building code that uses hyperparameters. This eliminates the need to write boilerplate code and helps make the code more maintainable.
Let's look at a very simple example.
Import kerastuner as ktimport tensorflow as tf
# initialize a parameter container hp = kt.HyperParameters () # define a modeldef build_model (hp): model = tf.keras.Sequential () model.add (tf.keras.layers.Dense (units=hp.Int ('units', min_value=32, max_value=512, step=32), activation='relu')) model.add (layers.Dense (10, activation='softmax')) model.compile (optimizer=tf.keras.optimizers.Adam (hp.Choice (' learning_rate', values= [1e-2, 1e-3) 1e-4]), loss='sparse_categorical_crossentropy', metrics= ['accuracy']) return model# wraps a random searcher tuner = kt.tuners.RandomSearch (build_model, objective='val_accuracy', max_trials=5, executions_per_trial=3, directory='my_dir') Project_name='helloworld') # print summary of search space tuner.search_space_summary () # search for the best hyperparametric configuration tuner.search (x, y, epochs=5, validation_data= (val_x, val_y) # retrieve the best model models= tuner.get_best_models (num_models=2) # print result summary tuner.results_summary ()
As you can see, kerastuner is so easy to use, a concise api method to define random parameters and training models.
After reading the above, do you have any further understanding of how to tune the hyperparameters of the neural network model in Keras? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.