我正在尝试使用 keras 功能 API 构建神经网络并训练我使用 keras 调谐器的网络。该模型由一些嵌入层和一些密集层组成:
import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Input, Embedding, Dense, Flatten
from tensorflow import keras
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.utils import plot_model
import matplotlib.pyplot as plt
from kerastuner.tuners import RandomSearch, BayesianOptimization
def build_model(hp):
model = keras.Sequential()
activation = hp.Choice('activation',['relu','tanh','linear'])
optimizer = hp.Choice('optimizer', ['adam', 'sgd', 'rmsprop'])
in_layers = list()
em_layers = list()
for i in range(len(X_train_enc)):
# calculate the number of unique inputs
n_labels = len(np.unique(X_train_enc[i]))
# define input layer
in_layer = Input(shape=(1,))
# define embedding layer
em_layer = Embedding(n_labels, round(n_labels/2))(in_layer)
# store layers
in_layers.append(in_layer)
em_layers.append(em_layer)
merge = keras.layers.concatenate(em_layers)
x = Flatten()(merge)
for i in range(hp.Int('num_layers', 1, 6)):
units = hp.Int(
'units_' + str(i),
min_value=8,
max_value=128,
step=16
)
x = Dense(units, activation=activation)(x)
drop_rate = hp.Choice('drop_rate_' + str(i),
[
0.0, 0.1, 0.2, 0.3, 0.4,
0.5, 0.6, 0.7, 0.8, 0.9
])
x = keras.layers.Dropout(rate=drop_rate)(x)
output = Dense(1, activation='linear')(x)
model = keras.models.Model(inputs=in_layers, outputs=output)
model.compile(
optimizer=optimizer,
loss=keras.losses.MeanSquaredError(reduction="auto", name="mean_squared_error"),
metrics=['accuracy']
)
return model
要使用调谐器,请执行以下代码:
tuner = BayesianOptimization(
build_model,
objective='accuracy',
max_trials=25,
executions_per_trial=5,
directory='drive/MyDrive/Master/train_model/nn_first_reg',
project_name='nn_bayes_first_reg',
seed=10)
tuner.search(X_train_enc,y_train)
tuner.results_summary()
best_hyperparameters = tuner.get_best_hyperparameters(1)[0]
model = tuner.hypermodel.build(best_hyperparameters)
history = model.fit(X_train_enc, y_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,))
调谐器的结果用于构建模型。
_, accuracy = model.evaluate(X_test_enc, y_test)
print('Accuracy: %.2f' % (accuracy*100))
print(model.summary())
from tensorflow.keras.utils import plot_model
plot_model(model, to_file='drive/MyDrive/Master/plots/results/before_game/nn_first.png')
model.summary()
和的输出plot_model
为空。输出如下所示:
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
Total params: 137,329
Trainable params: 137,329
Non-trainable params: 0
__________________________________________________________________________________________________
None
不知道这是否已连接,但代码运行得非常好,我只是绘制网络以查看它是如何构建的。但是,当我尝试保存模型时,我确实遇到了错误:
model.save('drive/MyDrive/Master/SavedModels/nn_first_before.csv')
错误是:
KeyError: 'input_269_ib-0'
我不知道这些是否相关。