1

Keras を使用して LSTM ネットワークを開発しています。「gridsearchcv」を使用してパラメーターを最適化しています。エポックパラメーターをグリッドサーチしたくないため、「早期停止」機能を導入することにしました。残念ながら、"delta_min" を非常に大きく、"patience" を非常に低く設定しても、トレーニングは停止しません。トレーニング フェーズが Earlystopping コールバックを無視しているようです。

gridsearchcv と Earlystopping は互​​換性がないのでしょうか?

私のコードの下:

import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import LSTM, Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import make_scorer

from sklearn.model_selection import GridSearchCV
from time import time

#for earlystop implementation
from keras.callbacks import EarlyStopping

def create_model(optimizer, hl1_nodes, input_shape):
    # creation of the NN - Electric Load
    # LSTM layers followed by other LSTM layer must have the parameter "return_sequences" set at True
    model = Sequential()
    model.add(LSTM(units = hl1_nodes , input_shape=input_shape, return_sequences=False))
    model.add(Dense(1, activation="linear"))  # output layer
    model.compile(optimizer=optimizer, loss='mean_squared_error', metrics=['mean_absolute_error'])
    model.summary()
    return model

def LSTM_1HL_method(X_train, X_test, Y_train, Y_test):

    # normalize X and Y data
    mmsx = MinMaxScaler()
    mmsy = MinMaxScaler()

    X_train = mmsx.fit_transform(X_train)
    X_test = mmsx.transform(X_test)
    Y_train = mmsy.fit_transform(Y_train)
    Y_test = mmsy.transform(Y_test)  

    # NN for Electric Load
    # LSTM  Input Shape
    time_steps = 1  # number of time-steps you are feeding a sequence (?)
    inputs_numb = X_train.shape[1]  # number of inputs
    input_shape=(time_steps, inputs_numb)

    model = KerasRegressor(build_fn=create_model,verbose=1,input_shape=input_shape)

    #GridSearch code
    start=time()
    optimizers = ['adam']
    epochs = np.array([1000])
    hl1_nodes = np.array([32, 64, 128])
    btcsz = np.array([1,X_train.shape[0]])

    earlyStop=[EarlyStopping(monitor="loss",verbose=1,mode='min',min_delta=1000,patience=1)] #early stop setting


    param_grid = dict(optimizer=optimizers, hl1_nodes=hl1_nodes, nb_epoch=epochs,batch_size=btcsz, callbacks=[earlyStop])
    scoring = make_scorer(mean_squared_error) #in order to use a metric as a scorer
    grid = GridSearchCV(estimator=model, param_grid=param_grid, scoring = scoring)

    # NN training
    X_train = X_train.reshape(X_train.shape[0], 1, X_train.shape[1])
    grid_result = grid.fit(X_train, Y_train)

    # Predictions - Electric Load
    Yhat_train = grid_result.predict(X_train)
    X_test = X_test.reshape(X_test.shape[0], 1, X_test.shape[1])
    Yhat_test = grid_result.predict(X_test)


    # Denormalization - Electric Load
    Yhat_train=Yhat_train.reshape(-1,1)
    Yhat_test=Yhat_test.reshape(-1,1)
    Yhat_train = mmsy.inverse_transform(Yhat_train)
    Yhat_test = mmsy.inverse_transform(Yhat_test)


    return Yhat_train, Yhat_test
4

1 に答える 1