无法在Hyperas中使用中间功能

时间:2018-12-14 10:02:41

标签: python keras hyperas

我正在尝试使用Hyperas优化我的keras模型,但是我一直在NameError: processing (function_name) is not defined.中从Hyperas看过thisthis示例,并做到了这一点。它似乎对我不起作用。

这是我的代码:

def processing():
    df = pd.read_json('balanced_all.json')

    def label (df):
        if df['rating'] < 3:
            return 0
        if df['rating'] > 3:
            return 1

    df['label'] = df.apply (lambda df: label(df), axis=1)

    df = df[['review_text', 'label']]

    maxlen = 100
    max_words = 2000

    tokenizer = Tokenizer(num_words=max_words)
    tokenizer.fit_on_texts(df['review_text'].values)
    sequences = tokenizer.texts_to_sequences(df['review_text'].values)
    word_index = tokenizer.word_index


    sequences = pad_sequences(sequences, maxlen=maxlen)
    labels = pd.get_dummies(df['label']).values

    glove_dir = '/home/uttam/Documents/Thesis/Glove'
    embeddings_index = {}
    f = open(os.path.join(glove_dir, 'glove.6B.100d.txt'), 'r', encoding='utf-8')
    for line in f:
        values = line.split()
        word = values[0]
        coefs = np.asarray(values[1:], dtype='float32')
        embeddings_index[word] = coefs
    f.close()

    embedding_dim = 100


    embedding_matrix = np.zeros((max_words, embedding_dim))
    for word, i in word_index.items():
        if i < max_words:
            embedding_vector = embeddings_index.get(word)
            if embedding_vector is not None:
                embedding_matrix[i] = embedding_vector

    return sequences, labels, embedding_matrix



def data():
    sequences = processing()[0]
    labels = processing()[1]
    x_train, x_test, y_train, y_test = train_test_split(sequences,labels, test_size = 0.33, random_state = 42)
    return x_train, y_train, x_test, y_test



def create_model(x_train, y_train, x_test, y_test):
    embedding_dim = 100
    max_words = 2000
    embedding_matrix = processing()[2]


    model = Sequential()
    model.add(Embedding(max_words, embedding_dim, input_length=100))
    model.add(LSTM(128))
    model.add(Dropout({{uniform(0, 1)}}))
    model.add(Dense(2, activation='sigmoid'))

    model.layers[0].set_weights([embedding_matrix])
    model.layers[0].trainable = False


    model.compile(optimizer={{choice(['rmsprop', 'adam', 'sgd'])}}, loss='binary_crossentropy',metrics=['acc'])
    result = model.fit(x_train, y_train, epochs=20, batch_size={{choice([64, 128])}}, validation_split=0.2)
    model.save('pre_trained_glove_model.h5')


    validation_acc = np.amax(result.history['val_acc'])
    print('Best validation acc of epoch:', validation_acc)
    return {'loss': -validation_acc, 'status': STATUS_OK, 'model': model}


if __name__ == '__main__':
    best_run, best_model = optim.minimize(model=create_model,
                                          data=data,
                                          algo=tpe.suggest,
                                          max_evals=5,
                                          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)

我什至不需要中间函数,我不得不创建它,因为hyperas找不到全局变量。例如如果我在hyperas函数之外有一个变量x,则说create_model(),它会说NameError: x is not defined

我需要这个,因为如您所见,我正在使用预先训练的手套嵌入。我无法将所有内容都放在data()create_model()中。例如data()需要变量sequenceslabel,而create_model需要变量embedding_matrix,因此(据我所知)无法拆分所有内容两个功能。

对我来说唯一可行的方法是将所有内容同时放入data()create_model()函数中,这肯定不是有效的方法,也不是这样做的方法。

1 个答案:

答案 0 :(得分:1)

有点晚了,但是为了将来参考,您是正确的hyperas无法识别全局变量。您可以在minimize的函数列表中传递该函数:

best_run, best_model = optim.minimize(model=create_model,
                                      data=data,
                                      functions=[processing], # <<
                                      algo=tpe.suggest,
                                      max_evals=5,
                                      trials=Trials())

如前所述,是否需要在hyperas中传递全局变量。您可以选择以下选项之一:

使用data()

def data():
    ## ... my  code ...
    return x_train, y_train, x_test, y_test, foo

def create_model(x_train, y_train, x_test, y_test, foo):

或定义一个新函数并将其传递到函数列表中:

def my_funct():
    return foo

def data():
   return x_train, y_train, x_test, y_test

def create_model(x_train, y_train, x_test, y_test):
    foo = my_funct()

best_run, best_model = optim.minimize(model=create_model,
                                      data=data,
                                      functions=[my_funct], # << foo
                                      algo=tpe.suggest,
                                      max_evals=5,
                                      trials=Trials())