def __init__(self, small, batch_size): BaseRecommender.__init__(self, small, batch_size) self.user_num = self.reader.get_user_num() self.prod_num = self.reader.get_prod_num() self.batch_num_train = self.reader.get_batch_num_train() self.batch_num_test = self.reader.get_batch_num_test() self.epoch_num = 1 #50 print "Number of Users : %d\tNumber of Prods : %d" \ % (self.user_num, self.prod_num) print "Number of train batches : %d\tNumber of test batches : %d" \ % (self.batch_num_train, self.batch_num_test)
def __init__(self, small, batch_size): BaseRecommender.__init__(self, small, batch_size) self.user_num = self.reader.get_user_num() self.prod_num = self.reader.get_prod_num() self.batch_num_train = self.reader.get_batch_num_train() self.batch_num_test = self.reader.get_batch_num_test() print("Number of Users : %d\tNumber of Prods : %d" % (self.user_num, self.prod_num)) print "Number of train batches : {:d}\tNumber of test batches : {:d}" \ .format(self.batch_num_train, self.batch_num_test) self.prods_profile = None self.users_profile = None
def __init__(self, small, batch_size): BaseRecommender.__init__(self, small, batch_size) self.user_num = self.reader.get_user_num() self.prod_num = self.reader.get_prod_num() self.batch_num_train = self.reader.get_batch_num_train() self.batch_num_test = self.reader.get_batch_num_test() print "Number of Users : %d\tNumber of Prods : %d" \ % (self.user_num, self.prod_num) print "Number of train batches : %d\tNumber of test batches : %d" \ % (self.batch_num_train, self.batch_num_test) self.overall_avg_score = None self.prod_avg = None self.similarity_matrix = None
def __init__(self, small, batch_size, factor_num, wd = 0.001): BaseRecommender.__init__(self, small, batch_size) self.user_num = self.reader.get_user_num() self.prod_num = self.reader.get_prod_num() self.batch_num_train = self.reader.get_batch_num_train() self.batch_num_test = self.reader.get_batch_num_test() self.factor_num = factor_num self.user_dic = {} self.epoch_num = 20 self.wd = 0 print "Number of Users : %d\tNumber of Prods : %d" \ % (self.user_num, self.prod_num) print "Number of train batches : %d\tNumber of test batches : %d" \ % (self.batch_num_train, self.batch_num_test)
def __init__(self, small, batch_size, factor_num): BaseRecommender.__init__(self, small, batch_size) self.user_num = self.reader.get_user_num() self.prod_num = self.reader.get_prod_num() self.batch_num_train = self.reader.get_batch_num_train() self.batch_num_test = self.reader.get_batch_num_test() self.factor_num = factor_num self.user_dic = {} self.epoch_num = 3 #50 print("Number of Users : %d\tNumber of Prods : %d" % (self.user_num, self.prod_num)) print("Number of train batches : %d\tNumber of test batches : %d" % (self.batch_num_train, self.batch_num_test)) def build(self): ("\n" " build recommender system\n" " ") # Variables self.Wp = tf.Variable(tf.random_uniform([self.prod_num, self.factor_num], -1.0, 1.0), name='Wp') self.Wu = tf.Variable(tf.random_uniform([self.user_num, self.factor_num], -1.0, 1.0), name='Wu') self.bp = tf.Variable(tf.zeros([self.prod_num]), name='bp') self.bu = tf.Variable(tf.zeros([self.user_num]), name='bu') self.b = tf.Variable(0.0, name='b') # Placeholders self.u_ind = tf.placeholder('int32', shape=[self.batch_size]) self.y_rate = tf.placeholder('float32', shape=[self.batch_size, self.prod_num]) self.y_mask = tf.placeholder('float32', shape=[self.batch_size, self.prod_num]) # models for i in range(self.batch_size): ind = tf.gather(self.u_ind, i) currU = tf.gather(self.Wu, ind) currBu = tf.reshape(tf.gather(self.bu, ind), [1, 1]) if i==0: U = currU Bu = currBu else: U = tf.concat(0, [U, currU]) Bu = tf.concat(0, [Bu, currBu]) # U : [batch_size, factor_num] # Wp : [prod_num, factor_num] # Bu : [batch_size] Umat = tf.reshape(U, [self.batch_size, self.factor_num]) Pmat = tf.transpose(self.Wp) biasU = tf.reshape(tf.tile(tf.reshape(Bu, [self.batch_size]), [self.prod_num]), [self.batch_size, self.prod_num]) biasP = tf.transpose(tf.reshape(tf.tile(self.bp, [self.batch_size]), [self.prod_num, self.batch_size])) b_expand = tf.expand_dims(self.b, 0) bias = tf.reshape(tf.tile(b_expand, [self.batch_size*self.prod_num]), [self.batch_size, self.prod_num]) self.pred_y_rate = tf.matmul(Umat, Pmat) + biasU + biasP + bias loss_m = tf.sqrt(tf.squared_difference(self.y_rate, self.pred_y_rate)) self.loss = tf.reduce_sum(loss_m * self.y_mask)/self.batch_size optimizer = tf.train.AdamOptimizer(0.01) train_op = optimizer.minimize(self.loss) #training self.sess = tf.Session() self.sess.run(tf.initialize_all_variables()) for epoch in range(self.epoch_num): t = time.time() tot_loss = 0.0 for i in range(self.batch_num_train): _, loss = self.sess.run([train_op, self.loss], feed_dict = self.get_feed_dict(self.reader.get_next_train())) tot_loss += loss print (loss) avg_loss = tot_loss / self.batch_num_train print ("Epoch %d\tLoss\t%.2f\tTime %dmin" \ % (epoch, avg_loss, (time.time()-t)/60)) print ("Recommender is built!")