Exemplo n.º 1
0
    Y_train = RNN_Demo.predict(train_input)
    Y_train = Y_train[0, :, 0]

    # get test_result
    Y_pred = RNN_Demo.predict(test_input)
    Y_pred = Y_pred[0, :, 0]
    # Y_target = test_target

    # get prediction loss
    MSE_loss = nn.MSELoss()
    Y_pred_torch = Variable(torch.from_numpy(Y_pred).float(),
                            requires_grad=False)
    Y_target_torch = test_target
    MSE_pred = MSE_loss(Y_pred_torch, Y_target_torch)
    MSE_pred = MSE_pred.data.numpy()

    # # print forecast
    # for i in range(len(test)):
    #     print('Predicted=%f, Expected=%f' % ( y_pred[i], raw_values[-len(test)+i]))

    plot_fig_name = './Results/'+Cell + '_L' + \
        str(Num_layers) + '_H' + str(Hidden_size) + \
        '_I' + str(Num_iters)+'_'+Optim_method
    plot_regression_result(Train_target=train_target_plot,
                           Test_target=test_target_plot,
                           Train_pred=Y_train,
                           Test_pred=Y_pred,
                           Loss_pred=MSE_pred,
                           Fig_name=plot_fig_name)
Exemplo n.º 2
0
def RNN_Benchmark(input_dim, Hidden_size, output_dim, Num_layers, RNN_Cell,
                  Num_iters, Optim_method, Learning_rate, Print_interval,
                  Plot_interval, train_input, train_target, validate_input,
                  validate_target, test_input, test_target):
    RNN_Demo = RNNModel(input_dim=1,
                        hidden_size=Hidden_size,
                        output_dim=1,
                        num_layers=Num_layers,
                        cell=RNN_Cell,
                        num_iters=Num_iters,
                        optim_method=Optim_method,
                        learning_rate=Learning_rate,
                        print_interval=Print_interval,
                        plot_interval=Plot_interval).cuda()
    # ========================================================================================
    Save_Road = './Results/DS1'
    RNN_Demo.fit_validate(train_input,
                          train_target,
                          validate_input,
                          validate_target,
                          save_road=Save_Road)

    # RNN_Demo.fit(train_input, train_target,View_interval)
    # save the model
    # model_save_road='./Model/Model' + '_L' + str(Num_layers) + '_H' + str(Hidden_size) + '_I' + str(Num_iters)+Optim_method+'.pkl'
    # torch.save(RNN_Demo,model_save_road)

    # RNN_Demo=Model_ViewList[0]
    # Train_ViewList = Model_ViewList[1]
    # ---------------------------------------------------------------------------------------
    # begin to forcast
    print('\n------------------------------------------------')
    print('Forecasting Testing Data')
    print('------------------------------------------------')

    Y_train = RNN_Demo.predict(train_input)
    Y_train = Y_train[0, :, 0]

    # get test_result
    Y_pred = RNN_Demo.predict(test_input)
    Y_pred = Y_pred[0, :, 0]
    # Y_target = test_target

    # get prediction loss
    MSE_loss = nn.MSELoss()
    Y_pred_torch = torch.from_numpy(Y_pred).float()
    Y_target_torch = test_target
    MSE_pred = MSE_loss(Y_pred_torch, Y_target_torch)
    MSE_pred = MSE_pred.data.numpy()
    RMSE_pred = np.sqrt(MSE_pred)

    # # print forecast
    # for i in range(len(test)):
    #     print('Predicted=%f, Expected=%f' % ( y_pred[i], raw_values[-len(test)+i]))

    plot_fig_name = Save_Road+'_Pred_'+RNN_Cell + '_L' + \
        str(Num_layers) + '_H' + str(Hidden_size) + \
        '_E' + str(Num_iters)+'_'+Optim_method

    train_target_plot = train_target.data.numpy()[0, :, 0]
    test_target_plot = test_target.data.numpy()[0, :, 0]

    plot_regression_result(Train_target=train_target_plot,
                           Test_target=test_target_plot,
                           Train_pred=Y_train,
                           Test_pred=Y_pred,
                           Loss_pred=RMSE_pred,
                           Fig_name=plot_fig_name)