Beispiel #1
0
def getModelCNNLSTM(sequence, window_size=3, n_features=1, epochs=100):
    model = Sequential()
    model.add(
        Conv1D(filters=24,
               kernel_size=2,
               activation='relu',
               input_shape=(window_size, n_features)))
    model.add(MaxPooling1D(pool_size=2))
    model.add(
        LSTM(units=10,
             activation='tanh',
             recurrent_activation='hard_sigmoid',
             use_bias=True,
             kernel_initializer='glorot_uniform'))

    model.add(Dense(50, activation='relu'))
    model.add(Dense(1))
    model.compile(optimizer='adam', loss='mse')

    X, y = rollingwindow.split_sequence(sequence, window_size)
    X = X.reshape((X.shape[0], X.shape[1], n_features))
    es = EarlyStopping(monitor='val_loss',
                       min_delta=0.001,
                       patience=0,
                       verbose=0,
                       mode='auto',
                       baseline=None,
                       restore_best_weights=False)
    model.fit(X, y, epochs=epochs, callbacks=[es])

    return model
Beispiel #2
0
def getModelCNNWindow24(sequence,
                        window_size=int(24),
                        activation='relu',
                        n_features=1,
                        epochs=200):

    kernel_size = [2, 2]
    filters = [6, 6]
    pool_size = [2, 2]

    model = Sequential()
    model.add(
        Conv1D(filters=filters[0],
               kernel_size=kernel_size[0],
               activation='relu',
               input_shape=(window_size, n_features)))
    model.add(MaxPooling1D(pool_size=pool_size[0]))
    model.add(Activation('relu'))
    model.add(
        Conv1D(filters=filters[1],
               kernel_size=kernel_size[1],
               activation='relu',
               input_shape=(window_size, n_features)))
    model.add(MaxPooling1D(pool_size=pool_size[1]))
    model.add(Activation('relu'))

    model.add(Flatten())
    model.add(Dense(50, activation='relu'))
    model.add(Dense(1))

    model.compile(optimizer='adam', loss='mse')
    X, y = rollingwindow.split_sequence(sequence, window_size)
    X = X.reshape((X.shape[0], X.shape[1], n_features))

    es = EarlyStopping(monitor='val_loss',
                       min_delta=0,
                       patience=0,
                       verbose=0,
                       mode='auto',
                       baseline=None,
                       restore_best_weights=False)
    model.fit(X, y, epochs=epochs, callbacks=[es])

    return model
def main():


    if len(sys.argv) != 5:
        quit("ERROR:\nParameters: <direction> <interfaceID> <algorithm> <length_series>")

    inOrOut = sys.argv[1]#options: "in", "out"
    interfaceid = int(sys.argv[2])#option: whatever is in your orion database for interfaceids
    algorithm = sys.argv[3]#options: cnn3, cnn24, lstm, lstmcnn
    length_series = int(sys.argv[4])#option: integer(above 400 in length)

    ins, insTrains, outs, outsTrains = orionconnection.getInterface(length_series, interfaceid)

    sequence = ins
    trains = insTrains

    n_features = 1
    window_size = 3

    if inOrOut == "out":
        sequence = outs
        trains = outsTrains   
    
    if algorithm == "cnn3":
        model = forecaster.getModelCNNWindow3(sequence)
    elif algorithm == "cnn24":
        window_size = 24
        model = forecaster.getModelCNNWindow24(sequence, window_size, n_features)
    elif algorithm == "lstm":
        model = forecaster.getModelLSTM(sequence)
    elif algorithm == "lstmcnn":
        model = forecaster.getModelLSTM(sequence)
    
    filename, weightfile = createFilenames(inOrOut, interfaceid, algorithm)
    
    forecaster.saveModel(model, filename, weightfile)

    X, y = rollingwindow.split_sequence(trains, window_size)
    X = X.reshape((X.shape[0], X.shape[1], n_features))

    print(model.evaluate(X, y))