def train_one_epoch(self, epoch): uid, iid, lb = gtl.shuffle_list(self.train_uid, self.train_iid, self.train_labels) n_batches, total_loss, total_mae, total_rms = 0, 0, 0, 0 for i in range(self.num_batch): batch_user = uid[i * self.batch_size:(i + 1) * self.batch_size] batch_item = iid[i * self.batch_size:(i + 1) * self.batch_size] batch_labels = lb[i * self.batch_size:(i + 1) * self.batch_size] _, l, mae, rms = self.session.run( [self.opt, self.loss, self.mae, self.rms], feed_dict={ self.uid: batch_user, self.iid: batch_item, self.labels: batch_labels }) n_batches += 1 total_loss += l total_mae += mae total_rms += rms if self.verbose: if n_batches % self.skip_step == 0: print( "Epoch {0} Batch {1}: [Loss] = {2} [MAE] = {3}".format( epoch, n_batches, total_loss / n_batches, total_mae / n_batches)) if self.verbose: print("Epoch {0}: [Loss] {1}".format(epoch, total_loss / n_batches)) print("Epoch {0}: [MAE] {1} and [RMS] {2}".format( epoch, total_mae / n_batches, total_rms / n_batches))
def train_one_epoch(self, epoch): uid, iid, lb = gtl.shuffle_list(self.train_uid, self.train_iid, self.train_labels) neg_iid = [random.choice(self.neg_dict[u]) for u in uid] # neg_iid = [np.random.choice(self.neg_dict[u], 1).item() for u in self.train_uid] #This is very slow # neg_iid = [self.neg_dict[u][np.random.randint(len(self.neg_dict[u]))] for u in uid] #This is much faster n_batches, total_loss = 0, 0 for i in range(self.num_batch): batch_user = uid[i * self.batch_size:(i + 1) * self.batch_size] batch_item = iid[i * self.batch_size:(i + 1) * self.batch_size] batch_labels = lb[i * self.batch_size:(i + 1) * self.batch_size] batch_neg_iid = neg_iid[i * self.batch_size:(i + 1) * self.batch_size] # Randomly select one negative item j for each user # batch_neg_iid = [np.random.choice(self.neg_dict[u],1).item() for u in batch_user] _, l = self.session.run( [self.opt, self.loss], feed_dict={ self.uid: batch_user, self.iid: batch_item, self.neg_iid: batch_neg_iid }) n_batches += 1 total_loss += l if self.verbose: if n_batches % self.skip_step == 0: print("Epoch {0} Batch {1}: [Loss] = {2}".format( epoch, n_batches, total_loss / n_batches)) if self.verbose: print("Epoch {0}: [Loss] {1}".format(epoch, total_loss / n_batches))
def train_one_epoch(self, epoch): uid, iid = gtl.shuffle_list(self.train_uid, self.train_iid) # start_time = time.time() iid_neg = [random.choice(self.neg_dict[u]) for u in uid] # iid_neg = [np.random.choice(self.neg_dict[u]).item() for u in uid] # iid_neg = [self.neg_dict[u][np.random.randint(len(self.neg_dict[u]))] for u in uid] # print("Time={0}".format(time.time()-start_time)) n_batches, total_loss = 0, 0 for i in range(self.num_batch): if i == self.num_batch - 1: # break batch_uids = uid[i * self.batch_size:] batch_iids_pos = iid[i * self.batch_size:] batch_iids_neg = iid_neg[i * self.batch_size:] else: batch_uids = uid[i * self.batch_size:(i + 1) * self.batch_size] batch_iids_pos = iid[i * self.batch_size:(i + 1) * self.batch_size] batch_iids_neg = iid_neg[i * self.batch_size:(i + 1) * self.batch_size] # Randomly select one negative item j for each user # batch_iids_neg = [np.random.choice(self.neg_dict[u], 1).item() for u in batch_uids] feed_dict = { self.uid: batch_uids, self.pos_iid: batch_iids_pos, self.neg_iid: batch_iids_neg, } if self.is_adv: self.session.run([self.update_P, self.update_Q], feed_dict) _, l = self.session.run([self.optimizer, self.loss], feed_dict) n_batches += 1 total_loss += l if self.verbose: if n_batches % self.skip_step == 0: print("[All] Training Epoch {0} Batch {1}: [Loss] = {2}". format(epoch, n_batches, total_loss / n_batches)) if self.verbose: print("[Epoch Average] Training Epoch {0}: [Loss] {1}".format( epoch, total_loss / n_batches))
def train_one_epoch(self, epoch): uid1, iid1, lb1, uid2, iid2, lb2 = gtl.shuffle_list( self.train_uid1, self.train_iid1, self.train_labels1, self.train_uid2, self.train_iid2, self.train_labels2) # uid1, iid1, lb1, uid2, iid2, lb2 = list(uid1), list(iid1), list(lb1), list(uid2), list(iid2), list(lb2) n_batches = 0 total_loss = 0 total_mae1 = 0 total_mae2 = 0 for i in range(self.num_batch): batch_user1 = uid1[i * self.batch_size:(i + 1) * self.batch_size] batch_user2 = uid2[i * self.batch_size:(i + 1) * self.batch_size] batch_item1 = iid1[i * self.batch_size:(i + 1) * self.batch_size] batch_item2 = iid2[i * self.batch_size:(i + 1) * self.batch_size] batch_labels1 = lb1[i * self.batch_size:(i + 1) * self.batch_size] batch_labels2 = lb2[i * self.batch_size:(i + 1) * self.batch_size] _, l, mae1, mae2 = self.session.run( [self.opt, self.loss, self.mae1, self.mae2], feed_dict={ self.dom1_uid: batch_user1, self.dom2_uid: batch_user2, self.dom1_iid: batch_item1, self.dom2_iid: batch_item2, self.dom1_labels: batch_labels1, self.dom2_labels: batch_labels2 }) n_batches += 1 total_loss += l total_mae1 += mae1 total_mae2 += mae2 # total_mae1 += evl.evalMAE(batch_labels1,np.round(pred1)) # total_mae2 += evl.evalMAE(batch_labels2,np.round(pred2)) if self.verbose: if n_batches % self.skip_step == 0: print( "Epoch {0} Batch {1}: [Loss] = {2} [MAE] = {3}".format( epoch, n_batches, total_loss / n_batches, (total_mae1 + total_mae2) / (2 * n_batches))) print("Epoch {0}: [Loss] {1}".format(epoch, total_loss / n_batches)) print("Epoch {0}: [MAE] {1} and {2}".format(epoch, total_mae1 / n_batches, total_mae2 / n_batches))