我正在尝试使用 keras hyperas 优化 IMBD 数据集的超参数,但出现错误。我用这个(https://www.kaggle.com/kt66nf/hyperparameter-optimization-using-keras-hyperas)代码作为参考
代码
def vectorize_sequences(sequences, dimension=10000):
# Create an all-zero matrix of shape (len(sequences), dimension)
results = np.zeros((len(sequences), dimension))
for i, sequence in enumerate(sequences):
results[i, sequence] = 1. # set specific indices of results[i] to 1s
return results
def data():
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
x_train = vectorize_sequences(train_data) #vectorized training data
x_test = vectorize_sequences(test_data)
y_train = np.asarray(train_labels).astype('float32') # vectorized labels
y_test = np.asarray(test_labels).astype('float32')
return x_train, y_train, x_test, y_test
def create_model(x_train, y_train, x_test, y_test):
model = models.Sequential()
model.add(layers.Dense(16, activation='relu', input_shape=(10000,)))
model.add(layers.Dense(16, activation='relu'))
model.add(Dropout({{uniform(0.5, 1)}}))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(64, activation='relu'))
model.add(Dropout({{uniform(0.5, 1)}}))
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', metrics=['accuracy'],
optimizer={{choice(['rmsprop', 'adam', 'sgd'])}})
model.fit(x_train, y_train,
batch_size={{choice([16, 32, 64])}},
epochs={{choice([25, 50, 75, 100])}},
validation_data=(x_test, y_test))
score, acc = model.evaluate(x_test, y_test, verbose=0)
print('Test accuracy:', acc)
return {'loss': -acc, 'status': STATUS_OK, 'model': model}
***then I loaded google drive and set the path to the notebook
best_run, best_model = optim.minimize(model= create_model,
data=data,
max_evals=15,
algo=tpe.suggest,
notebook_name='Copy of imbd', #name of the notebook
trials= Trials())
这最后一个代码是我收到错误“文件”,第 182 行] ^ SyntaxError:无效语法的地方