Hyperopt детерминированная модель Keras с семенами - PullRequest
1 голос
/ 02 июля 2019

Я пытаюсь использовать hyperopt для классификационной модели глубокого обучения с кератами:

import numpy as np
import tensorflow as tf
import random as rn
# The below is necessary for starting Numpy generated random numbers
# in a well-defined initial state.
np.random.seed(1)

# The below is necessary for starting core Python generated random numbers
# in a well-defined state.
rn.seed(2)

# Force TensorFlow to use single thread.
# Multiple threads are a potential source of non-reproducible results.
session_conf = tf.ConfigProto(intra_op_parallelism_threads=1,
                              inter_op_parallelism_threads=1)
from keras import backend as K
tf.set_random_seed(2)
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)

#importing libraries
import ...

from hyperas import optim
from hyperas.distributions import choice, uniform, randint
from hyperopt import Trials, STATUS_OK, tpe

def data():

    return x_train_sequence, y_train, x_test_sequence, y_test


# ===============
# Model creation
# ===============

def create_model(x_train_sequence, y_train, x_test_sequence, y_test):

    embedding_dim = {{choice([...])}}
    lstm = {{choice([...])}}
    num_epochs = {{choice([...])}}
    dropout = {{uniform(0, 1)}}
    batch_size = {{choice([32])}}
    model = Sequential()
    model.add(...)
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=["binary_accuracy"])

    # Fit the model and evaluate
    result = model.fit(x_train_sequence, y_train,
batch_size=batch_size,
                        validation_data=(x_test_sequence, y_test), verbose=verbose, shuffle=True, epochs=num_epochs)
    return {'loss': -validation_acc, 'status': STATUS_OK, 'model': model}

# ===============
# Apply model and find best run
# ===============

if __name__ == '__main__':

    best_run, best_model = optim.minimize(model=create_model,
                                          data=data,
                                          algo=tpe.suggest,
                                          rseed=np.random.seed(1),
                                          max_evals=50,
                                          trials=Trials())
    X_train, Y_train, X_test, Y_test = data()
    print("Evalutation of best performing model:")
    print(best_model.evaluate(X_test, Y_test))
    print("Best performing model chosen hyper-parameters:")
    print(best_run)

Даже при том, что я думал, что я положил все необходимые семена для получения воспроизводимых случаев.Я продолжаю получать разные результаты, даже если я заменяю {{choice ([...])}} целыми числами.

Чего мне не хватает?Что я должен добавить, чтобы правильно посеять модель?

Заранее большое спасибо!

...