示例#1
0
class DisplayCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        clear_output(wait=True)
        show_predictions()
        print('\nSample Prediction after epoch {}\n'.format(epoch + 1))


EPOCHS = 3
VAL_SUBSPLITS = 1
VALIDATION_STEPS = info.splits[
    'test'].num_examples // BATCH_SIZE // VAL_SUBSPLITS

model_history = model.fit(train,
                          epochs=EPOCHS,
                          steps_per_epoch=STEPS_PER_EPOCH,
                          validation_steps=VALIDATION_STEPS,
                          validation_data=test,
                          callbacks=[DisplayCallback()])

loss = model_history.history['loss']
val_loss = model_history.history['val_loss']

epochs = range(EPOCHS)

plt.figure()
plt.plot(epochs, loss, 'r', label='Training loss')
plt.plot(epochs, val_loss, 'bo', label='Validation loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss Value')
plt.ylim([0, 1])
示例#2
0
def build_model(data, config, output_length, X_test, input_length, index,
                repeats):
    print('Run:', str(index), '/', str(repeats))
    # start time
    start = time.time()
    print('Configuration:', config)
    # define parameters
    epochs, batch_size, n_nodes1, n_nodes2, filter1, filter2, kernel_size = config
    # convert data to supervised learning environment
    X_train = np.stack([
        sliding_window_input(data, input_length, i)
        for i in range(len(data) - input_length)
    ])[:-output_length]
    y_train = np.stack([
        sliding_window_output(data['Upper Stillwater'], input_length,
                              output_length, i)
        for i in range(len(data) - (input_length + output_length))
    ])
    y_train = y_train.reshape(y_train.shape[0], y_train.shape[1], 1)
    print('Feature Training Tensor (samples, timesteps, features):',
          X_train.shape)
    print('Target Training Tensor (samples, timesteps, features):',
          y_train.shape)
    # define parameters
    n_timesteps, n_features, n_outputs = X_train.shape[1], X_train.shape[
        2], y_train.shape[1]
    # define residual CNN-LSTM
    visible1 = Input(shape=(n_timesteps, n_features))

    model = Conv1D(filter1, kernel_size, padding='causal')(visible1)
    residual1 = ReLU()(model)
    model = Conv1D(filter1, kernel_size, padding='causal')(residual1)
    model = Add()([residual1, model])
    model = ReLU()(model)

    model = Conv1D(filter1, kernel_size, padding='causal')(model)
    residual2 = ReLU()(model)
    model = Conv1D(filter1, kernel_size, padding='causal')(residual2)
    model = Add()([residual2, model])
    model = ReLU()(model)
    model = MaxPooling1D()(model)

    model = Conv1D(filter2, kernel_size, padding='causal')(model)
    residual3 = ReLU()(model)
    model = Conv1D(filter2, kernel_size, padding='causal')(residual3)
    model = Add()([residual3, model])
    model = ReLU()(model)

    model = Conv1D(filter2, kernel_size, padding='causal')(model)
    residual4 = ReLU()(model)
    model = Conv1D(filter2, kernel_size, padding='causal')(residual4)
    model = Add()([residual4, model])
    model = ReLU()(model)
    model = MaxPooling1D()(model)

    model = Conv1D(n_nodes1, kernel_size, padding='causal')(model)
    residual5 = ReLU()(model)
    model = Conv1D(n_nodes1, kernel_size, padding='causal')(residual5)
    model = Add()([residual5, model])
    model = ReLU()(model)

    model = Conv1D(n_nodes1, kernel_size, padding='causal')(model)
    residual6 = ReLU()(model)
    model = Conv1D(n_nodes1, kernel_size, padding='causal')(residual6)
    model = Add()([residual6, model])
    model = ReLU()(model)
    model = MaxPooling1D()(model)

    model = Flatten()(model)
    model = RepeatVector(n_outputs)(model)

    model = LSTM(n_nodes1, activation='relu', return_sequences=True)(model)
    model = LSTM(n_nodes1, activation='relu', return_sequences=True)(model)
    model = LSTM(n_nodes1, activation='relu', return_sequences=True)(model)
    model = LSTM(n_nodes1, activation='relu', return_sequences=True)(model)

    dense = TimeDistributed(Dense(n_nodes2, activation='relu'))(model)
    output = TimeDistributed(Dense(1))(dense)
    model = Model(inputs=visible1, outputs=output)
    model.compile(loss='mse', optimizer='adam')
    #model.summary()
    # fit network
    es = EarlyStopping(monitor='loss', mode='min', patience=10)
    history = model.fit(X_train,
                        y_train,
                        epochs=epochs,
                        batch_size=batch_size,
                        verbose=0,
                        validation_split=0.2,
                        callbacks=[es])
    # test model against hold-out set
    y_pred = model.predict(X_test)
    print('\n Elapsed time:', round((time.time() - start) / 60, 3), 'minutes')
    return y_pred, history