-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimization.py
128 lines (105 loc) · 5.55 KB
/
optimization.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
# Import the Required Libraries
import numpy as np
from io import StringIO
import math
import random
# Minimzes the l2 error over user vectors keeping movie vector constant
def user_optimization(rating_matrix, user_vectors, movie_vectors, K):
# Set up the constants for use WHAT VALUES TO USE
# alpha is learning rate and lambda_r is regularization parameter
MAX_ITERATIONS = 5000
alpha = 0.005
lambda_r = 0.02
# Take the transpose of the item_vector array
movie_vectors = movie_vectors.T
for step in range(MAX_ITERATIONS):
for i in range(len(rating_matrix)):
for j in range(len(rating_matrix[i])):
# Only if User has rated the movie
if rating_matrix[i][j] > 0:
# Calculate the Error
error = rating_matrix[i][j] - np.dot(user_vectors[i,:],movie_vectors[:,j])
# Change the Vectors depending on the derivative, only change movie_vector: ALTERNATING OPTIMIZATION
for k in range(K):
user_vectors[i][k] = user_vectors[i][k] + alpha * (2 * error * movie_vectors[k][j] - lambda_r * user_vectors[i][k])
movie_vectors[k][j] = movie_vectors[k][j] + alpha * (2 * error * user_vectors[i][k] - lambda_r * movie_vectors[k][j])
# Calculate the Total Error
error = 0
for i in range(len(rating_matrix)):
for j in range(len(rating_matrix[i])):
if rating_matrix[i][j] > 0:
error = error + pow(rating_matrix[i][j] - np.dot(user_vectors[i,:],movie_vectors[:,j]), 2)
for k in range(K):
error = error + (lambda_r) * (pow(user_vectors[i][k],2))
print ("Error: ", error)
# Break if converged
if error < 0.001:
break
# return the movie profile vectors
return user_vectors, movie_vectors.T
# Minimzes the l2 error over movie vectors keeping user vectors constant
def movie_optimization(rating_matrix, user_vectors, movie_vectors, K):
# Set up the constants for use WHAT VALUES TO USE
# alpha is learning rate and lambda_r is regularization parameter
MAX_ITERATIONS = 5000
alpha = 0.0002
lambda_r = 0.02
# Take the transpose of the item_vector array
movie_vectors = movie_vectors.T
for step in range(MAX_ITERATIONS):
for i in range(len(rating_matrix)):
for j in range(len(rating_matrix[i])):
# Only if User has rated the movie
if rating_matrix[i][j] > 0:
# Calculate the Error
error = rating_matrix[i][j] - np.dot(user_vectors[i,:],movie_vectors[:,j])
# Change the Vectors depending on the derivative, only change movie_vector: ALTERNATING OPTIMIZATION
for k in range(K):
# user_vectors[i][k] = user_vectors[i][k] + alpha * (2 * error * movie_vectors[k][j] - lambda_r * user_vectors[i][k])
movie_vectors[k][j] = movie_vectors[k][j] + alpha * (2 * error * user_vectors[i][k] - lambda_r * movie_vectors[k][j])
# Calculate the Total Error
error = 0
for i in range(len(rating_matrix)):
for j in range(len(rating_matrix[i])):
if rating_matrix[i][j] > 0:
error = error + pow(rating_matrix[i][j] - np.dot(user_vectors[i,:],movie_vectors[:,j]), 2)
for k in range(K):
error = error + (lambda_r) * (pow(movie_vectors[k][j],2))
# Break if converged
if error < 0.01:
break
# return the movie profile vectors
return movie_vectors
# Minimzes the l2 error over user vectors keeping movie vector constant
def user_optimization_heirarchical(rating_matrix, user_vectors, movie_vectors, parent_user_vector, K):
# Set up the constants for use WHAT VALUES TO USE
# alpha is learning rate and lambda_r is regularization parameter
MAX_ITERATIONS = 5000
alpha = 0.0002
lambda_r = 0.02
# Take the transpose of the item_vector array
movie_vectors = movie_vectors.T
for step in range(MAX_ITERATIONS):
for i in range(len(rating_matrix)):
for j in range(len(rating_matrix[i])):
# Only if User has rated the movie
if rating_matrix[i][j] > 0:
# Calculate the Error
error = rating_matrix[i][j] - np.dot(user_vectors[i,:],movie_vectors[:,j])
# Change the Vectors depending on the derivative, only change movie_vector: ALTERNATING OPTIMIZATION
for k in range(K):
user_vectors[i][k] = user_vectors[i][k] + alpha * (2 * error * movie_vectors[k][j] - lambda_r * (user_vectors[i][k] - parent_user_vector[i][k]))
# movie_vectors[k][j] = movie_vectors[k][j] + alpha * (2 * error * user_vectors[i][k] - lambda_r * movie_vectors[k][j])
# Calculate the Total Error
error = 0
for i in range(len(rating_matrix)):
for j in range(len(rating_matrix[i])):
if rating_matrix[i][j] > 0:
error = error + pow(rating_matrix[i][j] - np.dot(user_vectors[i,:],movie_vectors[:,j]), 2)
for k in range(K):
error = error + (lambda_r) * (pow(user_vectors[i][k] - parent_user_vector[i][k],2))
# Break if converged
if error < 0.001:
break
# return the movie profile vectors
return user_vectors, movie_vectors.T