From d16c3abee5c1f9b3d443cfd30d67248612d6276d Mon Sep 17 00:00:00 2001 From: euisuk-chung Date: Mon, 13 Dec 2021 06:04:45 +0000 Subject: [PATCH] eval code update --- utils/TSTR.py | 37 +++++++++++++++++++++++++------------ utils/visual_vrae.py | 20 ++++++++++++++------ vrae_experiment.sh | 12 ++++++------ 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/utils/TSTR.py b/utils/TSTR.py index 70e5f0e..39b9b22 100644 --- a/utils/TSTR.py +++ b/utils/TSTR.py @@ -4,13 +4,14 @@ from sklearn.metrics import accuracy_score, mean_squared_error class GeneralRNN(torch.nn.Module): - r"""A general RNN model for time-series prediction + """ + A general RNN model for time-series prediction """ def __init__(self, args): super(GeneralRNN, self).__init__() - self.model_type = args['model_type'] + self.model_type = args['model_type'] self.input_size = args['in_dim'] self.hidden_size = args['h_dim'] self.output_size = args['out_dim'] @@ -86,13 +87,13 @@ def one_step_ahead_prediction(train_data, test_data): args["task"] = "regression" args["model_type"] = "gru" args["bidirectional"] = False - args["epochs"] = 20 + args["epochs"] = 2000 args["batch_size"] = 256 args["in_dim"] = dim - args["h_dim"] = dim + args["h_dim"] = 30 # dim args["out_dim"] = dim args["n_layers"] = 3 - args["dropout"] = 0.5 + args["dropout"] = 0.3 args["max_seq_len"] = 30 # only 29 is used for prediction args["learning_rate"] = 1e-3 args["grad_clip_norm"] = 5.0 @@ -110,6 +111,7 @@ def one_step_ahead_prediction(train_data, test_data): batch_size=no, shuffle=True ) + # Initialize model model = GeneralRNN(args) model.to(args["device"]) @@ -142,14 +144,25 @@ def one_step_ahead_prediction(train_data, test_data): # Evaluate the trained model with torch.no_grad(): - perf = 0 + perf = np.zeros((8789,2668)) + ind = 0 for test_x in test_dataloader: test_x = test_x.to(args["device"]) test_p = model(test_x[:,0:29,:]).cpu() - test_p = np.reshape(test_p.numpy(), [-1]) - test_y = np.reshape(test_x[:,1:30,:].cpu().numpy(), [-1]) - - perf += rmse_error(test_y, test_p) - - return perf \ No newline at end of file + test_p = np.reshape(test_p.numpy(), [test_p.numpy().shape[0],-1]) + test_y = np.reshape(test_x[:,1:30,:].cpu().numpy(), [test_x[:,1:30,:].cpu().numpy().shape[0],-1]) + + perf[ind:ind+test_x[:,1:30,:].cpu().numpy().shape[0]] = test_p-test_y + ind = ind+test_x[:,1:30,:].cpu().numpy().shape[0] + #perf += rmse_error(test_y, test_p) + #perf2.append(rmse_error(test_y, test_p)) + + mae = np.abs(perf).mean(1) + rmse = (perf * perf).mean(1) + rmse = np.sqrt(rmse) + + print(f'MAE : {mae.mean(0)}') + print(f'RMSE : {rmse.mean(0)}') + + return perf, rmse.mean(0), mae.mean(0) diff --git a/utils/visual_vrae.py b/utils/visual_vrae.py index 9abe416..5d40438 100644 --- a/utils/visual_vrae.py +++ b/utils/visual_vrae.py @@ -5,7 +5,7 @@ import pandas as pd -def visualization(original_data, generated_data, analysis): +def visualization(original_data, generated_data, analysis, name, x_zoom=False, y_zoom=False): """Using PCA or tSNE for generated and original data visualization. Args: @@ -44,14 +44,18 @@ def visualization(original_data, generated_data, analysis): ax.legend() - plt.title('PCA plot') - plt.xlabel('x-pca') - plt.ylabel('y-pca') + if x_zoom != False and y_zoom != False: + plt.xlim(x_zoom[0],x_zoom[1]) + plt.ylim(y_zoom[0],y_zoom[1]) + + plt.title(f'{name} PCA') + plt.xlabel('PC_1') + plt.ylabel('PC_2') plt.show() elif analysis == 'tsne': # TSNE Analysis - tsne = TSNE(n_components=2, verbose=1, perplexity=40, n_iter=300) + tsne = TSNE(n_components=2, verbose=1, perplexity=50, n_iter=1000) # conat data concat_data = np.concatenate((original_data, generated_data), axis=0) @@ -69,7 +73,11 @@ def visualization(original_data, generated_data, analysis): ax.legend() - plt.title('tsne plot') + if x_zoom != False and y_zoom != False: + plt.xlim(0,x_zoom) + plt.ylim(0,y_zoom) + + plt.title(f'{name} T-SNE') plt.xlabel('x-tsne') plt.ylabel('y-tsne') plt.show() \ No newline at end of file diff --git a/vrae_experiment.sh b/vrae_experiment.sh index 50f32b7..4aa0e82 100644 --- a/vrae_experiment.sh +++ b/vrae_experiment.sh @@ -5,10 +5,10 @@ python run_vrae.py --scale_type 'Standard' --hidden_layer_depth 1 python run_vrae.py --scale_type 'Standard' --hidden_layer_depth 2 -# MinMax -python run_vrae.py --scale_type 'MinMax' --hidden_layer_depth 1 -python run_vrae.py --scale_type 'MinMax' --hidden_layer_depth 2 +# # MinMax +# python run_vrae.py --scale_type 'MinMax' --hidden_layer_depth 1 +# python run_vrae.py --scale_type 'MinMax' --hidden_layer_depth 2 -# Robust -python run_vrae.py --scale_type 'Robust' --hidden_layer_depth 1 -python run_vrae.py --scale_type 'Robust' --hidden_layer_depth 2 \ No newline at end of file +# # Robust +# python run_vrae.py --scale_type 'Robust' --hidden_layer_depth 1 +# python run_vrae.py --scale_type 'Robust' --hidden_layer_depth 2 \ No newline at end of file