dataset = Dataset(config.filename) config.item_count = dataset.item_count config.user_count = dataset.user_count tf.logging.info("\n\n%s\n\n" % config) model = PairwiseGMF(config) sv = tf.train.Supervisor(logdir=None, save_model_secs=0, save_summaries_secs=0) sess = sv.prepare_or_wait_for_session( config=tf.ConfigProto(gpu_options=tf.GPUOptions( per_process_gpu_memory_fraction=0.1, allow_growth=True))) for i in range(FLAGS.iters): if sv.should_stop(): break progress = tqdm(enumerate(dataset.get_data(FLAGS.batch_size, False, FLAGS.neg)), dynamic_ncols=True, total=(dataset.train_size * FLAGS.neg) // FLAGS.batch_size) loss = [] for k, example in progress: feed = { model.input_users: example[:, 0], model.input_items: example[:, 1], model.input_items_negative: example[:, 2], } batch_loss, _ = sess.run([model.loss, model.train], feed) loss.append(batch_loss) progress.set_description(u"[{}] Loss: {:,.4f} » » » » ".format(i, batch_loss)) print("Epoch {}: Avg Loss/Batch {:<20,.6f}".format(i, np.mean(loss))) user_embed, item_embed, v = sess.run([model.user_memory.embeddings, model.item_memory.embeddings, model.v.w])
dataset = Dataset(config.filename) config.item_count = dataset.item_count config.user_count = dataset.user_count tf.logging.info("\n\n%s\n\n" % config) model = PairwiseGMF(config) sv = tf.train.Supervisor(logdir=None, save_model_secs=0, save_summaries_secs=0) sess = sv.prepare_or_wait_for_session(config=tf.ConfigProto( gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.1, allow_growth=True))) for i in range(FLAGS.iters): if sv.should_stop(): break progress = tqdm(enumerate( dataset.get_data(FLAGS.batch_size, False, FLAGS.neg)), dynamic_ncols=True, total=(dataset.train_size * FLAGS.neg) // FLAGS.batch_size) loss = [] for k, example in progress: feed = { model.input_users: example[:, 0], model.input_items: example[:, 1], model.input_items_negative: example[:, 2], } batch_loss, _ = sess.run([model.loss, model.train], feed) loss.append(batch_loss) progress.set_description(u"[{}] Loss: {:,.4f} » » » » ".format( i, batch_loss)) print("Epoch {}: Avg Loss/Batch {:<20,.6f}".format(i, np.mean(loss)))
config.user_count = dataset.user_count tf.logging.info("\n\n%s\n\n" % config) model = PairwiseGMF(config) sv = tf.train.Supervisor(logdir=None, save_model_secs=0, save_summaries_secs=0) sess = sv.prepare_or_wait_for_session( config=tf.ConfigProto(gpu_options=tf.GPUOptions( per_process_gpu_memory_fraction=0.1, allow_growth=True))) for i in range(FLAGS.iters): if sv.should_stop(): break progress = tqdm(enumerate(dataset.get_data(FLAGS.batch_size, False, FLAGS.neg)), dynamic_ncols=True, total=(dataset.train_size * FLAGS.neg) // FLAGS.batch_size) loss = [] for k, example in progress: feed = { model.input_users: example[:, 0], model.input_items: example[:, 1], model.input_items_negative: example[:, 2], model.input_positive_items_popularity: settings.Settings.normalized_popularity[example[:, 1]], # Added by LC model.input_negative_items_popularity: settings.Settings.normalized_popularity[example[:, 2]] # Added by LC } if settings.Settings.loss_type == 2: batch_loss, _, parameter_k = sess.run([model.loss, model.train, model.k], feed) else: parameter_k = 0 batch_loss, _ = sess.run([model.loss, model.train], feed)
sess.graph._unsafe_unfinalize() tf.logging.info('Loading Pretrained Embeddings.... from %s' % FLAGS.pretrain) sess.run([ model.user_memory.embeddings.assign(pretrain['user'] * 0.5), model.item_memory.embeddings.assign(pretrain['item'] * 0.5) ]) # Train Loop for i in range(FLAGS.iters): if sv.should_stop(): break # 对应参数含义def get_data(self, batch_size, neighborhood, neg_count): # tqdm是一个强大的终端进度条工具 progress = tqdm(enumerate( dataset.get_data(FLAGS.batch_size, True, FLAGS.neg)), dynamic_ncols=True, total=(dataset.train_size * FLAGS.neg) // FLAGS.batch_size) loss = [] for k, example in progress: ratings, pos_neighborhoods, pos_neighborhood_length, \ neg_neighborhoods, neg_neighborhood_length = example feed = { # 所有行的第0列 model.input_users: ratings[:, 0], # 所有行的第1列 model.input_items: ratings[:, 1], model.input_items_negative: ratings[:, 2], model.input_neighborhoods: pos_neighborhoods, model.input_neighborhood_lengths: pos_neighborhood_length, model.input_neighborhoods_negative: neg_neighborhoods,
sess.run([ model.user_memory.embeddings.assign(pretrain['user'] * 0.5), model.item_memory.embeddings.assign(pretrain['item'] * 0.5) ]) else: sess.graph._unsafe_unfinalize() # Train Loop results = [] for i in range(FLAGS.iters): if sv.should_stop(): break progress = tqdm(enumerate( dataset.get_data(FLAGS.batch_size, True, FLAGS.neg, use_popularity=use_popularity)), dynamic_ncols=True, total=(dataset.train_size * FLAGS.neg) // FLAGS.batch_size) loss = [] for k, example in progress: ratings, pos_neighborhoods, pos_neighborhood_length, neg_neighborhoods, neg_neighborhood_length = example feed = { model.input_users: ratings[:, 0], model.input_items: ratings[:, 1], model.input_positive_items_popularity: settings.Settings.normalized_popularity[ratings[:, 1]], # Added by LC
model.item_memory.embeddings.assign(pretrain['item'] * 0.5) ]) print('Embeddings loaded!') except: print('Embeddings not found!') sess.graph._unsafe_unfinalize() else: sess.graph._unsafe_unfinalize() # Train Loop results = [] for i in range(FLAGS.iters): if sv.should_stop(): break progress = tqdm(enumerate(dataset.get_data(FLAGS.batch_size, True, FLAGS.neg, use_popularity=use_popularity)), dynamic_ncols=True, total=(dataset.train_size * FLAGS.neg) // FLAGS.batch_size) loss = [] for k, example in progress: ratings, pos_neighborhoods, pos_neighborhood_length, neg_neighborhoods, neg_neighborhood_length = example # print('len(ratings[:, 0]):',len(ratings[:, 0])) # print('len(ratings[:, 1]):', len(ratings[:, 1])) # print('len(pos_neighborhoods):', len(pos_neighborhoods)) # print('len(neg_neighborhoods):', len(neg_neighborhoods)) feed = { model.input_users: ratings[:, 0], model.input_items: ratings[:, 1], model.input_positive_items_popularity: settings.Settings.normalized_popularity[ratings[:, 1]], # Added by LC