Skip to content

Commit

Permalink
eval code update
Browse files Browse the repository at this point in the history
  • Loading branch information
euisuk-chung committed Dec 13, 2021
1 parent 8f9e770 commit d16c3ab
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
37 changes: 25 additions & 12 deletions utils/TSTR.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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
Expand All @@ -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"])
Expand Down Expand Up @@ -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
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)
20 changes: 14 additions & 6 deletions utils/visual_vrae.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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()
12 changes: 6 additions & 6 deletions vrae_experiment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
# # Robust
# python run_vrae.py --scale_type 'Robust' --hidden_layer_depth 1
# python run_vrae.py --scale_type 'Robust' --hidden_layer_depth 2

0 comments on commit d16c3ab

Please sign in to comment.