-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
59 lines (45 loc) · 1.64 KB
/
test.py
File metadata and controls
59 lines (45 loc) · 1.64 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import torch
import pandas as pd
from torch.utils.data import DataLoader, TensorDataset
from model import CustomNN
def load_test_data(x_path, y_path):
X = pd.read_csv(x_path).values
y = pd.read_csv(y_path).values
X_tensor = torch.tensor(X, dtype=torch.float32)
y_tensor = torch.tensor(y, dtype=torch.float32)
dataset = TensorDataset(X_tensor, y_tensor)
return dataset
def evaluate_model(model, dataloader, criterion, device):
model.to(device)
model.eval()
total_loss = 0
with torch.no_grad():
for batch_X, batch_y in dataloader:
batch_X, batch_y = batch_X.to(device), batch_y.to(device)
predictions = model(batch_X)
loss = criterion(predictions, batch_y)
total_loss += loss.item()
avg_loss = total_loss / len(dataloader)
print(f"Test Loss: {avg_loss:.4f}")
def main():
# Paths to data
test_x_path = "rev_test_X_final.csv"
test_y_path = "test_y.csv"
# Load test data
dataset = load_test_data(test_x_path, test_y_path)
dataloader = DataLoader(dataset, batch_size=32, shuffle=False)
# Model configuration
input_dim = dataset[0][0].shape[0]
output_dim = dataset[0][1].shape[0]
model_path = "best_custom_nn_model.pth"
# Load the best model
model = CustomNN(input_dim, output_dim)
model.load_state_dict(torch.load(model_path))
# Device configuration
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Evaluation criterion
criterion = torch.nn.MSELoss()
# Evaluate the model
evaluate_model(model, dataloader, criterion, device)
if __name__ == "__main__":
main()