Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

eval.py 1.1 KB

You have to be logged in to leave a comment. Sign In
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
  1. """
  2. Evaluate model performance
  3. """
  4. import pickle
  5. import json
  6. import numpy as np
  7. from sklearn.metrics import accuracy_score
  8. import torch
  9. def eval_model():
  10. # Load test data
  11. print("Loading data and model...")
  12. test_data = np.load('./data/processed_test_data.npy')
  13. # Load trained model
  14. with open('./data/model.pkl', 'rb') as f:
  15. model = pickle.load(f)
  16. # Switch model to evaluation (inference) mode
  17. model.eval()
  18. print("done.")
  19. # Divide loaded data-set into data and labels
  20. labels = test_data[:, 0]
  21. data = torch.Tensor(test_data[:, 1:].reshape([test_data.shape[0], 1, 28, 28]))
  22. # Run model on test data
  23. print("Running model on test data...")
  24. predictions = model(data).max(1, keepdim=True)[1].cpu().data.numpy()
  25. print("done.")
  26. # Calculate metric scores
  27. print("Calculating metrics...")
  28. metrics = {'accuracy': accuracy_score(labels, predictions)}
  29. # Save metrics to json file
  30. with open('./metrics/eval.json', 'w') as f:
  31. json.dump(metrics, f)
  32. print("done.")
  33. if __name__ == '__main__':
  34. eval_model()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...