def train_model(self): for epoch in range(self.num_epochs): # Generate training instances if self.ispairwise.lower() == "true": user_input, item_input_pos, item_input_neg = data_gen._get_pairwise_all_data(self.dataset) else: user_input, item_input, lables = data_gen._get_pointwise_all_data(self.dataset, self.num_negatives) total_loss = 0.0 training_start_time = time() num_training_instances = len(user_input) for num_batch in np.arange(int(num_training_instances / self.batch_size)): if self.ispairwise.lower() == "true": bat_users, bat_items_pos, bat_items_neg = \ data_gen._get_pairwise_batch_data(user_input, item_input_pos, item_input_neg, num_batch, self.batch_size) feed_dict = {self.user_input: bat_users, self.item_input: bat_items_pos, self.item_input_neg: bat_items_neg} else: bat_users, bat_items, bat_lables = \ data_gen._get_pointwise_batch_data(user_input, item_input, lables, num_batch, self.batch_size) feed_dict = {self.user_input: bat_users, self.item_input: bat_items, self.lables: bat_lables} loss, _ = self.sess.run((self.loss, self.optimizer), feed_dict=feed_dict) total_loss += loss print("[iter %d : loss : %f, time: %f]" % ( epoch + 1, total_loss / num_training_instances, time() - training_start_time)) if epoch % self.verbose == 0: Evaluate.test_model(self, self.dataset)
def training_discriminator(self, user, item, label): num_training_instances = len(user) for num_batch in np.arange( int(num_training_instances / self.batch_size)): bat_users, bat_items, bat_lables = \ data_gen._get_pointwise_batch_data(user, item, label, num_batch, self.batch_size) feed = { self.discriminator.u: bat_users, self.discriminator.i: bat_items, self.discriminator.label: bat_lables } self.sess.run(self.discriminator.d_updates, feed_dict=feed)