-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.py
141 lines (105 loc) · 3.41 KB
/
validate.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import sys
import time
import numpy as np
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
# Calculates the validation error.
#
# The following file(s) are read:
# mean_ratings_[#].npy
# validate_[#].npy
# naive_me_[#].pth
# naive_ce_[#].pth
# naive_pa_[#].pth
#
# Run examples:
# python validate.py 10k
# python validate.py all
######################
# Parse command line #
######################
SPACER = '=' * 78
print('================================ Validation =================================')
assert(len(sys.argv) == 2)
customers_str = sys.argv[1]
assert(customers_str in ('1k', '10k', '25k', '100k', 'all'))
num_customers = {
'1k' : 1000,
'10k' : 10000,
'25k' : 25000,
'100k': 100000,
'all' : 480189
}[customers_str]
print('Number of customers: {:>8,}'.format(num_customers))
num_movies = 17770
print('Number of movies: {:>8,}'.format(num_movies))
print(SPACER)
########################
# Read validation data #
########################
filename_data = 'validate_{}.npy'.format(customers_str)
t0 = time.time()
data = np.load(filename_data)
customer_ids = torch.tensor(data[:,0], dtype=torch.long).view(-1)
movie_ids = torch.tensor(data[:,1], dtype=torch.long).view(-1)
ratings = torch.tensor(data[:,2], dtype=torch.float).view(-1)
num_points = data.shape[0]
t1 = time.time()
del data
print('Read {:,} data points from {} in {:.1f} s.'.format(num_points, filename_data, t1 - t0))
#####################
# Read mean ratings #
#####################
t0 = time.time()
filename_mr = 'mean_ratings_{}.npy'.format(customers_str)
mean_ratings = torch.tensor(np.load(filename_mr), dtype=torch.float)
mean_ratings = mean_ratings[movie_ids]
mean_all = torch.mean(mean_ratings)
t1 = time.time()
print('Read means from {} in {:.1f} s.'.format(filename_mr, t1 - t0))
print('Overall mean is {:.4f}'.format(mean_all))
#######################
# Model specification #
#######################
filename_me = 'naive_me_{}.pth'.format(customers_str)
filename_ce = 'naive_ce_{}.pth'.format(customers_str)
filename_pa = 'naive_pa_{}.pth'.format(customers_str)
dim_customers = 20 # customer embedding dimensions
dim_movies = 20 # movie embedding dimensions
t0 = time.time()
movie_embedding = nn.Embedding(num_movies, dim_movies)
movie_embedding.load_state_dict(torch.load(filename_me))
movie_embedding.eval()
customer_embedding = nn.Embedding(num_customers, dim_customers)
customer_embedding.load_state_dict(torch.load(filename_ce))
customer_embedding.eval()
predict_appeal = nn.Sequential(
nn.Linear(dim_customers + dim_movies, 100),
nn.ReLU(),
nn.Linear(100, 100),
nn.Tanh(),
nn.Linear(100, 10),
nn.Tanh(),
nn.Linear(10, 1)
)
predict_appeal.load_state_dict(torch.load(filename_pa))
predict_appeal.eval()
t1 = time.time()
print('Loaded models in {:.1f} s'.format(t1 - t0))
print(SPACER)
##############################
# Calculate validation error #
##############################
m = movie_embedding(movie_ids)
c = customer_embedding(customer_ids)
a = predict_appeal(torch.cat((c, m), dim=1)).view(num_points)
p = F.hardtanh(mean_ratings + a, 1.0, 5.0)
criterion = nn.MSELoss(reduction='mean')
mse = criterion(p, ratings).item()
rmse = np.sqrt(mse)
print('Validation MSE: {:.4f}'.format(mse))
print('Validation RMSE: {:.4f}'.format(rmse))