-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathAutoRec.py
195 lines (155 loc) · 7.61 KB
/
AutoRec.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import tensorflow as tf
import time
import numpy as np
import os
import math
class AutoRec():
def __init__(self,sess,args,
num_users,num_items,
R, mask_R, C, train_R, train_mask_R, test_R, test_mask_R,num_train_ratings,num_test_ratings,
user_train_set, item_train_set, user_test_set, item_test_set,
result_path):
self.sess = sess
self.args = args
self.num_users = num_users
self.num_items = num_items
self.R = R
self.mask_R = mask_R
self.C = C
self.train_R = train_R
self.train_mask_R = train_mask_R
self.test_R = test_R
self.test_mask_R = test_mask_R
self.num_train_ratings = num_train_ratings
self.num_test_ratings = num_test_ratings
self.user_train_set = user_train_set
self.item_train_set = item_train_set
self.user_test_set = user_test_set
self.item_test_set = item_test_set
self.hidden_neuron = args.hidden_neuron
self.train_epoch = args.train_epoch
self.batch_size = args.batch_size
self.num_batch = int(math.ceil(self.num_users / float(self.batch_size)))
self.base_lr = args.base_lr
self.optimizer_method = args.optimizer_method
self.display_step = args.display_step
self.random_seed = args.random_seed
self.global_step = tf.Variable(0, trainable=False)
self.decay_epoch_step = args.decay_epoch_step
self.decay_step = self.decay_epoch_step * self.num_batch
self.lr = tf.train.exponential_decay(self.base_lr, self.global_step,
self.decay_step, 0.96, staircase=True)
self.lambda_value = args.lambda_value
self.train_cost_list = []
self.test_cost_list = []
self.test_rmse_list = []
self.result_path = result_path
self.grad_clip = args.grad_clip
def run(self):
self.prepare_model()
init = tf.global_variables_initializer()
self.sess.run(init)
for epoch_itr in range(self.train_epoch):
self.train_model(epoch_itr)
self.test_model(epoch_itr)
self.make_records()
def prepare_model(self):
self.input_R = tf.placeholder(dtype=tf.float32, shape=[None, self.num_items], name="input_R")
self.input_mask_R = tf.placeholder(dtype=tf.float32, shape=[None, self.num_items], name="input_mask_R")
V = tf.get_variable(name="V", initializer=tf.truncated_normal(shape=[self.num_items, self.hidden_neuron],
mean=0, stddev=0.03),dtype=tf.float32)
W = tf.get_variable(name="W", initializer=tf.truncated_normal(shape=[self.hidden_neuron, self.num_items],
mean=0, stddev=0.03),dtype=tf.float32)
mu = tf.get_variable(name="mu", initializer=tf.zeros(shape=self.hidden_neuron),dtype=tf.float32)
b = tf.get_variable(name="b", initializer=tf.zeros(shape=self.num_items), dtype=tf.float32)
pre_Encoder = tf.matmul(self.input_R,V) + mu
self.Encoder = tf.nn.sigmoid(pre_Encoder)
pre_Decoder = tf.matmul(self.Encoder,W) + b
self.Decoder = tf.identity(pre_Decoder)
pre_rec_cost = tf.multiply((self.input_R - self.Decoder) , self.input_mask_R)
rec_cost = tf.square(self.l2_norm(pre_rec_cost))
pre_reg_cost = tf.square(self.l2_norm(W)) + tf.square(self.l2_norm(V))
reg_cost = self.lambda_value * 0.5 * pre_reg_cost
self.cost = rec_cost + reg_cost
if self.optimizer_method == "Adam":
optimizer = tf.train.AdamOptimizer(self.lr)
elif self.optimizer_method == "RMSProp":
optimizer = tf.train.RMSPropOptimizer(self.lr)
else:
raise ValueError("Optimizer Key ERROR")
if self.grad_clip:
gvs = optimizer.compute_gradients(self.cost)
capped_gvs = [(tf.clip_by_value(grad, -5., 5.), var) for grad, var in gvs]
self.optimizer = optimizer.apply_gradients(capped_gvs, global_step=self.global_step)
else:
self.optimizer = optimizer.minimize(self.cost, global_step=self.global_step)
def train_model(self,itr):
start_time = time.time()
random_perm_doc_idx = np.random.permutation(self.num_users)
batch_cost = 0
for i in range(self.num_batch):
if i == self.num_batch - 1:
batch_set_idx = random_perm_doc_idx[i * self.batch_size:]
elif i < self.num_batch - 1:
batch_set_idx = random_perm_doc_idx[i * self.batch_size : (i+1) * self.batch_size]
_, Cost = self.sess.run(
[self.optimizer, self.cost],
feed_dict={self.input_R: self.train_R[batch_set_idx, :],
self.input_mask_R: self.train_mask_R[batch_set_idx, :]})
batch_cost = batch_cost + Cost
self.train_cost_list.append(batch_cost)
if (itr+1) % self.display_step == 0:
print ("Training //", "Epoch %d //" % (itr), " Total cost = {:.2f}".format(batch_cost),
"Elapsed time : %d sec" % (time.time() - start_time))
def test_model(self,itr):
start_time = time.time()
Cost,Decoder = self.sess.run(
[self.cost,self.Decoder],
feed_dict={self.input_R: self.test_R,
self.input_mask_R: self.test_mask_R})
self.test_cost_list.append(Cost)
if (itr+1) % self.display_step == 0:
Estimated_R = Decoder.clip(min=1, max=5)
unseen_user_test_list = list(self.user_test_set - self.user_train_set)
unseen_item_test_list = list(self.item_test_set - self.item_train_set)
for user in unseen_user_test_list:
for item in unseen_item_test_list:
if self.test_mask_R[user,item] == 1: # exist in test set
Estimated_R[user,item] = 3
pre_numerator = np.multiply((Estimated_R - self.test_R), self.test_mask_R)
numerator = np.sum(np.square(pre_numerator))
denominator = self.num_test_ratings
RMSE = np.sqrt(numerator / float(denominator))
self.test_rmse_list.append(RMSE)
print ("Testing //", "Epoch %d //" % (itr), " Total cost = {:.2f}".format(Cost), " RMSE = {:.5f}".format(RMSE),
"Elapsed time : %d sec" % (time.time() - start_time))
print ("=" * 100)
def make_records(self):
if not os.path.exists(self.result_path):
os.makedirs(self.result_path)
basic_info = self.result_path + "basic_info.txt"
train_record = self.result_path + "train_record.txt"
test_record = self.result_path + "test_record.txt"
with open (train_record,'w') as f:
f.write(str("Cost:"))
f.write('\t')
for itr in range(len(self.train_cost_list)):
f.write(str(self.train_cost_list[itr]))
f.write('\t')
f.write('\n')
with open (test_record,'w') as g:
g.write(str("Cost:"))
g.write('\t')
for itr in range(len(self.test_cost_list)):
g.write(str(self.test_cost_list[itr]))
g.write('\t')
g.write('\n')
g.write(str("RMSE:"))
for itr in range(len(self.test_rmse_list)):
g.write(str(self.test_rmse_list[itr]))
g.write('\t')
g.write('\n')
with open(basic_info,'w') as h:
h.write(str(self.args))
def l2_norm(self,tensor):
return tf.sqrt(tf.reduce_sum(tf.square(tensor)))