-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathDistributeMF_Part.py
61 lines (44 loc) · 2.01 KB
/
DistributeMF_Part.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
import time
import copy
import numpy as np
from load_data import ratings_dict, item_id_list, user_id_list
from shared_parameter import *
def user_update(single_user_vector, user_rating_list, item_vector):
gradient = {}
for item_id, rate in user_rating_list:
error = rate - np.dot(single_user_vector, item_vector[item_id])
single_user_vector = single_user_vector - lr * (-2 * error * item_vector[item_id] + 2 * reg_u * single_user_vector)
gradient[item_id] = error * single_user_vector
return single_user_vector, gradient
def mse():
loss = []
for i in range(len(user_id_list)):
for r in range(len(ratings_dict[user_id_list[i]])):
item_id, rate = ratings_dict[user_id_list[i]][r]
error = (rate - np.dot(user_vector[i], item_vector[item_id])) ** 2
loss.append(error)
return np.mean(loss)
if __name__ == '__main__':
# Init process
user_vector = np.random.normal(size=[len(user_id_list), hidden_dim])
item_vector = np.random.normal(size=[len(item_id_list), hidden_dim])
start_time = time.time()
for iteration in range(max_iteration):
print('###################')
t = time.time()
# Step 2 User updates
gradient_from_user = []
for i in range(len(user_id_list)):
user_vector[i], gradient = user_update(user_vector[i], ratings_dict[user_id_list[i]], item_vector)
gradient_from_user.append(gradient)
# Step 3 Server update
tmp_item_vector = copy.deepcopy(item_vector)
for g in gradient_from_user:
for item_id in g:
item_vector[item_id] = item_vector[item_id] - lr * (-2 * g[item_id] + 2 * reg_v * item_vector[item_id])
if np.mean(np.abs(item_vector - tmp_item_vector)) < 1e-4:
print('Converged')
break
print('Time', time.time() - t, 's')
print('loss', mse())
print('Converged using', time.time() - start_time)