Как можно вернуть графики потерь из функции, используя керасы, и распечатать их как вспомогательные? - PullRequest
0 голосов
/ 05 июня 2019

Мне было интересно, как я могу вернуть hist, который обозначает историю в следующей функции после обучения 2 моделей (RNN и LSTM) и напечатать их функции потерь в подзаговорах:

def train_model(model_type):
    '''
    This code is parallelised and runs on each process
    It trains a model with different layer sizes (hyperparameters)
    It saves the model and returns the score (error)
    '''
    import time

    import numpy as np
    import pandas as pd
    import multiprocessing
    import matplotlib.pyplot as plt

    from keras.layers import LSTM, SimpleRNN, Dense, Activation
    from keras.models import Sequential
    from keras.callbacks import EarlyStopping, ReduceLROnPlateau
    from keras.layers.normalization import BatchNormalization

    print(f'Training a model: {model_type}')

    callbacks = [
        EarlyStopping(patience=10, verbose=1),
        ReduceLROnPlateau(factor=0.1, patience=3, min_lr=0.00001, verbose=1),
    ]

    model = Sequential()

    if model_type == 'rnn':
        model.add(SimpleRNN(units=1440, input_shape=(trainX.shape[1], trainX.shape[2])))
    elif model_type == 'lstm':
        model.add(LSTM(units=1440, input_shape=(trainX.shape[1], trainX.shape[2])))

    model.add(Dense(480))
    model.add(BatchNormalization())
    model.add(Activation('tanh'))
    model.compile(loss='mean_squared_error', optimizer='adam')
    model.fit(
        trainX,
        trainY,
        epochs=50,
        batch_size=20,
        validation_data=(testX, testY),
        verbose=1,
        callbacks=callbacks,
    )

    # predict
    Y_Train_pred = model.predict(trainX)
    Y_Test_pred = model.predict(testX)

    train_MSE = mean_squared_error(trainY, Y_Train_pred)
    test_MSE = mean_squared_error(testY, Y_Test_pred)

    # you can also return values eg. the eval score
    return {'type': model_type, 'train_MSE': train_MSE, 'test_MSE': test_MSE}

Я попробовал следующий код:

def train_model(model_type):

...
hist = model.fit(... )

# Return values eg. the eval score or plots history
    return {..., 'hist': hist}

num_workers = 2
model_types = ['rnn', 'lstm']
# guard in the main module to avoid creating subprocesses recursively.
if __name__ == "__main__":
     pool = multiprocessing.Pool(num_workers, init_worker)

    scores = pool.map(train_model, model_types  )
    for s in scores:
        #plot losses for RNN + LSTM
        f, ax = plt.subplots(figsize=(20, 15))
        plt.subplot(1, 2, 1)
        ax=plt.plot(s['hist'].history['loss']    ,label='Train loss')
        #ax=plt.plot(hist_RNN.history['loss']    ,label='Train loss')

        plt.subplot(1, 2, 2)
        #ax=plt.plot(hist_LSTM.history['loss']    ,label='Train loss')
        ax=plt.plot(s['hist'].history['loss']    ,label='Train loss')

        plt.subplots_adjust(top=0.80, bottom=0.38, left=0.12, right=0.90, hspace=0.37, wspace=0.28)
        plt.savefig('_All_Losses_history_.png')
        plt.show()

print(scores)

Обычно я хотел бы выделить независимое имя модели, например plt.plot(hist_RNN...) и plt.plot(hist_LSTM...), так как я комментирую его, чтобы я мог вызывать / передавать их независимо, но так как оба модели RNN и LSTM одинаковы, чтобы уменьшить код, который я не делал этого, и теперь я ищу элегантный способ вернуть эти графики и напечатать их в любом месте в конце сюжета! Любая помощь будет принята с благодарностью.

1 Ответ

0 голосов
/ 05 июня 2019
print(history.history.keys())
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])

Вы можете назначить этих других, как history.history ['loss'], и поиграть с ними.

...