-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
36 lines (28 loc) · 1.18 KB
/
evaluate.py
File metadata and controls
36 lines (28 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import matplotlib.pyplot as plt
from sklearn.metrics import mean_absolute_error, mean_squared_error
import math
import torch
def evaluate_model(model, val_loader, device='cpu', pred_len=6, feature_idx=0, n_plot=3):
model.load_state_dict(torch.load("best_transformer_model.pth"))
model.eval()
X_val_batch, y_val_batch = next(iter(val_loader))
X_val_batch = X_val_batch.to(device)
y_val_batch = y_val_batch.to(device)
with torch.no_grad():
y_pred_batch = model(X_val_batch)
y_true = y_val_batch.cpu().numpy()
y_pred = y_pred_batch.cpu().numpy()
mae = mean_absolute_error(y_true.flatten(), y_pred.flatten())
rmse = math.sqrt(mean_squared_error(y_true.flatten(), y_pred.flatten()))
print(f"MAE: {mae:.4f}")
print(f"RMSE: {rmse:.4f}")
for i in range(n_plot):
plt.figure(figsize=(10, 4))
plt.plot(range(pred_len), y_true[i, :, feature_idx], label='True')
plt.plot(range(pred_len), y_pred[i, :, feature_idx], label='Predicted')
plt.title(f"Sample {i} - Feature {feature_idx}")
plt.xlabel("Prediction step")
plt.ylabel("Value (kW)")
plt.legend()
plt.grid(True)
plt.show()