1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 09_keras_Tuner使用keras Tuner调整超参数(超参数优化)

09_keras_Tuner使用keras Tuner调整超参数(超参数优化)

时间:2019-01-08 22:22:29

相关推荐

09_keras_Tuner使用keras Tuner调整超参数(超参数优化)

"""09_keras_Tuner使用keras Tuner调整超参数"""import tensorflow as tffrom tensorflow import kerasimport keras_tuner as kt#加载数据集(img_train, label_train),\(img_test, label_test)\=keras.datasets.fashion_mnist.load_data()# Normalize pixel values between 0 and 1img_train = img_train.astype('float32') / 255.0img_test = img_test.astype('float32') / 255.0#定义模型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-512hp_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.0001hp_learning_rate = hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])pile(optimizer=keras.optimizers.Adam(learning_rate=hp_learning_rate),loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['accuracy'])return model"""Keras Tuner 提供了四种调节器:RandomSearch、Hyperband、BayesianOptimization和Sklearn"""#实例化调节器并执行超调tuner = kt.Hyperband(model_builder,objective='val_accuracy',max_epochs=10,factor=3,directory='my_dir',project_name='intro_to_kt')tuner.search(img_train, label_train, epochs=50, validation_split=0.2, callbacks=[stop_early])# Get the optimal hyperparametersbest_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-connectedlayer is {best_hps.get('units')} and the optimal learning rate for the optimizeris {best_hps.get('learning_rate')}.""")# 生命达到特定值后提前停止训练stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_loss',patience=5)# 运行超参数搜索tuner.search(img_train, label_train, epochs=50, validation_split=0.2, callbacks=[stop_early])# Get the optimal hyperparametersbest_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-connectedlayer is {best_hps.get('units')} and the optimal learning rate for the optimizeris {best_hps.get('learning_rate')}.""")#训练模型"""使用从搜索中获得的超参数找到训练模型的最佳生命周期数。"""# Build the model with the optimal hyperparameters and train it on the data for 50 epochsmodel = 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)) + 1print('Best epoch: %d' % (best_epoch,))#最佳循环数不再进行训练hypermodel = tuner.hypermodel.build(best_hps)# Retrain the modelhypermodel.fit(img_train, label_train, epochs=best_epoch, validation_split=0.2)#测试数据上评估超模型eval_result = hypermodel.evaluate(img_test, label_test)print("[test loss, test accuracy]:", eval_result)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。