import lstm import parameters import plot %load_ext autoreload %autoreload 2 # Initialization of seeds set_random_seed(2) seed(2) (params, _, yraw, y, yhat, num_errors) =\ lstm.predict('params_3y_1L256_09i.yaml') plot.prediction(y, yhat, yraw, num_errors, params, inv_scale=False, inv_diff=False, inv_log=False) # # -- Averaging predictions. # # y_avg = ((yhat1 + yhat2 + yhat3) / 3.0) # rmse, num_errors_avg = compute.error(y1, y_avg) # plot.prediction(y1, y_avg, num_errors_avg, params1) # # -- Single value prediction. # # prediction = lstm.single_predict(model1, X_test[31], Y_test[31], params) # print(prediction)
raw = data.read(params) print('Original dataset num samples:', raw.shape) adjusted = parameters.adjust(raw, params) X_train, Y_train, X_test, Y_test = data.prepare(adjusted, params) # Build the model and train it. # model = lstm.load('../../data/networks/20180115_0832.h5') model = lstm.build(params) train_loss = lstm.fit(model, X_train, Y_train, params) plot.history(train_loss) # Plot the test values for Y, and Y_hat, without scaling (inverted) Y_hat = model.predict(X_test, batch_size=params['lstm_batch_size']) rmse, num_errors = compute.error(Y_test, Y_hat) plot.prediction(params['y_scaler'].inverse_transform(Y_test), params['y_scaler'].inverse_transform(Y_hat), num_errors) # Save the model saved_model_name = lstm.save(model) # How to make a prediction # We need a vector of size (timesteps x num_features) # That vector is replicated 'batch_size' times, and feed into the network # The result is an array o 'batch_size' predictions, and we're only interested # in the first one, which is the one we give back p = repeat(X_test[0], 8).reshape((8, 6, 8)) p.shape model.predict(p)[0] Y_hat[0]
# s e t u p # params = parameters.read() adjusted = parameters.adjust(read(params), params) X, Y, Xtest, ytest = prepare(normalize(adjusted, params), params) # # t r a i n i n g # model = setup(params) parameters.summary(params) model.summary() lstm.stateless_fit(model, X, Y, Xtest, ytest, params) EarlyStopping( monitor='val_loss', min_delta=0.0, patience=0, verbose=0, mode='auto') save(model, params, prefix='5y', additional_epocs=0) # # r e b u i l d & p r e d i c t # pred = lstm.build(params, batch_size=1) pred.set_weights(model.get_weights()) (yhat, rmse, num_errors) = lstm.range_predict(pred, Xtest, ytest, params) # # p l o t # # plot.history(train_loss) plot.prediction(ytest, yhat, rmse, num_errors, params)