Created
October 17, 2018 10:45
-
-
Save romanovzky/a7fd3a7ede09c8c81b06bc5965c34402 to your computer and use it in GitHub Desktop.
hyperas GRU naming mistake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.datasets import imdb | |
from keras import preprocessing | |
from keras.layers import Dense, Embedding, SimpleRNN, GRU, LSTM, Dropout | |
from keras.models import Sequential | |
from sklearn.model_selection import train_test_split | |
from sklearn.metrics import recall_score | |
from hyperas import optim | |
from hyperas.distributions import choice, uniform | |
from hyperopt import Trials, STATUS_OK, tpe | |
import numpy as np | |
def data(): | |
max_features = 10000 | |
maxlen = 500 | |
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features) | |
x_train = preprocessing.sequence.pad_sequences(x_train, maxlen=maxlen) | |
x_test = preprocessing.sequence.pad_sequences(x_test, maxlen=maxlen) | |
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.5) | |
return x_train, y_train, x_val, y_val | |
def create_model(x_train, y_train, x_val, y_val): | |
model = Sequential() | |
model.add(Embedding(10000, 32)) | |
model.add(GRU( | |
{{choice([8,16,32])}}, | |
recurrent_dropout={{uniform(0,1)}}, | |
dropout={{uniform(0,1)}})) | |
model.add(Dense( | |
units={{choice([8,16,32])}}, | |
activation='relu')) | |
model.add(Dropout( | |
{{uniform(0,1)}})) | |
model.add(Dense(1, activation='sigmoid')) | |
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc']) | |
_ = model.fit(x_train, | |
y_train, | |
batch_size=128, | |
epochs=1,#{{choice([1,2])}}, | |
verbose=2) | |
y_pred = model.predict(x_val) | |
y_scores = y_pred.reshape(-1) | |
y_pred = np.array([ round(score) for score in y_scores]) | |
recall = recall_score(y_pred=y_pred, y_true=y_val) | |
return {'loss': -recall, 'status': STATUS_OK, 'model': model} | |
if __name__ == '__main__': | |
best_run, best_model = optim.minimize(model=create_model, | |
data=data, | |
algo=tpe.suggest, | |
max_evals=2, | |
trials=Trials(), | |
notebook_name='test') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment