Пример #1
0
def training_batch(batch_index, model, sess, batches):
    for index in batch_index:
        user_input, num_idx, item_input, labels = data.batch_gen(batches, index)
        feed_dict = {model.user_input: user_input, model.num_idx: num_idx[:, None],
                     model.item_input: item_input[:, None],
                     model.labels: labels[:, None]}
        sess.run([model.loss, model.optimizer], feed_dict)
Пример #2
0
def training(flag, model, dataset, epochs, num_negatives):
    batch_begin = time()
    batches = data.shuffle(dataset, args.batch_choice, num_negatives)
    batch_time = time() - batch_begin

    num_batch = len(batches[1])
    batch_index = range(num_batch)

    #train by epoch
    for epoch_count in range(epochs):
        train_loss = training_loss(model, batches)
        print(epoch_count, train_loss)
        batch_begin = time()
        batches = data.shuffle(dataset, model.batch_choice, num_negatives)
        #  np.random.shuffle(batch_index)
        batch_time = time() - batch_begin
Пример #3
0
def training_loss(model, sess, batches):
    train_loss = 0.0
    num_batch = len(batches[1])
    for index in range(num_batch):
        user_input, num_idx, item_input, labels = data.batch_gen(batches, index)
        feed_dict = {model.user_input: user_input, model.num_idx: num_idx[:, None], model.item_input: item_input[:, None],model.labels: labels[:, None]}
        train_loss += sess.run(model.loss, feed_dict)
    return train_loss / num_batch
Пример #4
0
def training_loss(model, batches):
    train_loss = 0.0
    num_batch = len(batches[1])
    optimizer = optim.Adam(model.parameters(), lr=0.01)

    for index in range(num_batch):
        user_input, num_idx, item_input, labels = data.batch_gen(
            batches, index)

        user_input = torch.Tensor(user_input)
        num_idx = torch.Tensor(num_idx)
        item_input = torch.Tensor(item_input)
        labels = torch.Tensor(labels)

        optimizer.zero_grad()
        loss = model(user_input, num_idx, item_input, labels)
        print("epoch %d : loss %f" % (index + 1, loss))
        train_loss += loss
        loss.backward()
        optimizer.step()
    return train_loss / num_batch
Пример #5
0
def training(flag, model, dataset, epochs, num_negatives):
    saver = tf.train.Saver({
        'c1': model.c1,
        'embedding_Q': model.embedding_Q,
        'bias': model.bias
    })
    weight_path = 'Pretraining/%s/alpha%.1f' % (model.dataset_name,
                                                model.data_alpha)

    with tf.Session() as sess:
        # pretrain nor not
        if flag != 0:
            ckpt = tf.train.get_checkpoint_state(
                os.path.dirname(weight_path + '/checkpoint'))
            if ckpt and ckpt.model_checkpoint_path:
                sess.run(tf.global_variables_initializer())
                saver.restore(sess, ckpt.model_checkpoint_path)
                logging.info("using pretrained variables")
                print("using pretrained variables")
            else:
                sess.run(tf.global_variables_initializer())
                logging.info("initialized")
                print("initialized")
        else:
            sess.run(tf.global_variables_initializer())
            logging.info("initialized")
            print("initialized")

        # initialize for training batches
        batch_begin = time()
        batches = data.shuffle(dataset, model.batch_choice, num_negatives)
        batch_time = time() - batch_begin

        num_batch = len(batches[1])
        batch_index = list(range(num_batch))

        # initialize the evaluation feed_dicts
        testDict = evaluate.init_evaluate_model(model, sess,
                                                dataset.testRatings,
                                                dataset.testNegatives,
                                                dataset.trainList)

        # train by epoch
        for epoch_count in range(epochs):

            train_begin = time()
            training_batch(batch_index, model, sess, batches)
            train_time = time() - train_begin

            if epoch_count % model.verbose == 0:

                if model.train_loss:
                    loss_begin = time()
                    train_loss = training_loss(model, sess, batches)
                    loss_time = time() - loss_begin
                else:
                    loss_time, train_loss = 0, 0

                eval_begin = time()
                (hits, ndcgs,
                 losses) = evaluate.eval(model, sess, dataset.testRatings,
                                         dataset.testNegatives, testDict)
                hr, ndcg, test_loss = np.array(hits).mean(), np.array(
                    ndcgs).mean(), np.array(losses).mean()
                eval_time = time() - eval_begin

                logging.info(
                    "Epoch %d [%.1fs + %.1fs]: HR = %.4f, NDCG = %.4f, loss = %.4f [%.1fs] train_loss = %.4f [%.1fs]"
                    % (epoch_count, batch_time, train_time, hr, ndcg,
                       test_loss, eval_time, train_loss, loss_time))
                print(
                    "Epoch %d [%.1fs + %.1fs]: HR = %.4f, NDCG = %.4f, loss = %.4f [%.1fs] train_loss = %.4f [%.1fs]"
                    % (epoch_count, batch_time, train_time, hr, ndcg,
                       test_loss, eval_time, train_loss, loss_time))

            batch_begin = time()
            batches = data.shuffle(dataset, model.batch_choice, num_negatives)
            np.random.shuffle(batch_index)
            batch_time = time() - batch_begin
Пример #6
0
def training(flag, model, dataset, epochs, num_negatives, attention_filename):

    saver = tf.train.Saver({
        'c1': model.c1,
        'embedding_Q': model.embedding_Q,
        'bias': model.bias
    })
    weight_path = 'Pretraining/%s/embed_size%d' % (model.dataset_name,
                                                   model.embedding_size)

    if model.ckpt:
        if model.algorithm:
            model_name = "NAIS_concat"
        else:
            model_name = "NAIS_prod"
        ckpt_path = "Model_weights/%s/%s/embed_size%d" % (
            model_name, model.dataset_name, model.embedding_size)
        if not os.path.exists(ckpt_path):
            os.makedirs(ckpt_path)
        ckpt_saver = tf.train.Saver()
        ckpt_path = ckpt_path + "/weights"
    with tf.Session() as sess:
        # pretrain nor not
        if flag != 0:
            ckpt = tf.train.get_checkpoint_state(
                os.path.dirname(weight_path + '/checkpoint'))
            if ckpt and ckpt.model_checkpoint_path:
                sess.run(tf.global_variables_initializer())
                saver.restore(sess, ckpt.model_checkpoint_path)
                logging.info("using pretrained variables")
                print "using pretrained variables"
        else:
            sess.run(tf.global_variables_initializer())
            logging.info("initialized")
            print "initialized"

        #initialize for training batches
        batch_begin = time()
        batches = data.shuffle(dataset, model.batch_choice, num_negatives)
        batch_time = time() - batch_begin

        num_batch = len(batches[1])
        batch_index = range(num_batch)

        #initialize the evaluation feed_dicts
        testDict = evaluate.init_evaluate_model(model, sess,
                                                dataset.testRatings,
                                                dataset.testNegatives,
                                                dataset.trainList)

        #train by epoch
        for epoch_count in range(epochs):

            train_begin = time()
            training_batch(batch_index, model, sess, batches)
            train_time = time() - train_begin

            if epoch_count % model.verbose == 0:

                if model.train_loss:
                    loss_begin = time()
                    train_loss = training_loss(model, sess, batches)
                    loss_time = time() - loss_begin
                else:
                    loss_time, train_loss = 0, 0

                eval_begin = time()
                (hits, ndcgs, losses,
                 attention) = evaluate.eval(model, sess, dataset.testRatings,
                                            dataset.testNegatives, testDict)
                hr, ndcg, test_loss = np.array(hits).mean(), np.array(
                    ndcgs).mean(), np.array(losses).mean()
                eval_time = time() - eval_begin

                logging.info(
                    "Epoch %d [%.1fs + %.1fs]: HR = %.4f, NDCG = %.4f, loss = %.4f [%.1fs] train_loss = %.4f [%.1fs]"
                    % (epoch_count, batch_time, train_time, hr, ndcg,
                       test_loss, eval_time, train_loss, loss_time))
                print "Epoch %d [%.1fs + %.1fs]: HR = %.4f, NDCG = %.4f, loss = %.4f [%.1fs] train_loss = %.4f [%.1fs]" % (
                    epoch_count, batch_time, train_time, hr, ndcg, test_loss,
                    eval_time, train_loss, loss_time)
                if model.logAttention:
                    weight_file_mean = open(
                        attention_filename + ('epoch%d' % epoch_count) +
                        "mean", 'w')
                    weight_file_var = open(
                        attention_filename + ('epoch%d' % epoch_count) + "var",
                        'w')
                    for row in attention[0]:
                        print >> weight_file_mean, "%.4f" % np.mean(row)
                        print >> weight_file_var, "%.4f" % np.var(row)
                    weight_file_mean.close()
                    weight_file_var.close()
                    weight_file_mean_ = open(
                        attention_filename + ('epoch%d' % epoch_count) +
                        "mean_not", 'w')
                    weight_file_var_ = open(
                        attention_filename + ('epoch%d' % epoch_count) +
                        "var_not", 'w')
                    for row in attention[1]:
                        print >> weight_file_mean_, "%.4f" % np.mean(row)
                        print >> weight_file_var_, "%.4f" % np.var(row)
                    weight_file_mean_.close()
                    weight_file_var_.close()

            batch_begin = time()
            batches = data.shuffle(dataset, model.batch_choice, num_negatives)
            np.random.shuffle(batch_index)
            batch_time = time() - batch_begin

            if model.ckpt and epoch_count % 5 == 0:
                ckpt_saver.save(sess, ckpt_path, global_step=epoch_count)
Пример #7
0
def training(flag, model, dataset, epochs, num_negatives):
    weight_path = 'Pretraining/%s/%s/alpha0.0.ckpt' % (model.dataset_name,
                                                       model.embedding_size)
    saver = tf.train.Saver([model.c1, model.embedding_Q, model.bias])

    with tf.Session() as sess:
        # pretrain nor not
        if flag != 0:
            sess.run(tf.global_variables_initializer())
            saver.restore(sess, weight_path)
            p_c1, p_e_Q, p_b = sess.run(
                [model.c1, model.embedding_Q, model.bias])

            model.c1 = tf.Variable(p_c1,
                                   dtype=tf.float32,
                                   trainable=True,
                                   name='c1')
            model.embedding_Q_ = tf.concat([model.c1, model.c2],
                                           0,
                                           name='embedding_Q_')
            model.embedding_Q = tf.Variable(p_e_Q,
                                            dtype=tf.float32,
                                            trainable=True,
                                            name='embedding_Q')
            model.bias = tf.Variable(p_b,
                                     dtype=tf.float32,
                                     trainable=True,
                                     name='embedding_Q')

            logging.info("using pretrained variables")
            print("using pretrained variables")
        else:
            sess.run(tf.global_variables_initializer())
            logging.info("initialized")
            print("initialized")

        # initialize for training batches
        batch_begin = time()
        batches = data.shuffle(dataset, model.batch_choice, num_negatives)
        batch_time = time() - batch_begin

        num_batch = len(batches[1])
        batch_index = list(range(num_batch))

        # initialize the evaluation feed_dicts
        testDict = evaluate.init_evaluate_model(model, sess,
                                                dataset.testRatings,
                                                dataset.testNegatives,
                                                dataset.trainList)

        best_hr, best_ndcg = 0, 0
        # train by epoch
        for epoch_count in range(epochs):

            train_begin = time()
            training_batch(batch_index, model, sess, batches)
            train_time = time() - train_begin

            if epoch_count % model.verbose == 0:

                if model.train_loss:
                    loss_begin = time()
                    train_loss = training_loss(model, sess, batches)
                    loss_time = time() - loss_begin
                else:
                    loss_time, train_loss = 0, 0

                eval_begin = time()
                (hits, ndcgs,
                 losses) = evaluate.eval(model, sess, dataset.testRatings,
                                         dataset.testNegatives, testDict)
                hr, ndcg, test_loss = np.array(hits).mean(), np.array(
                    ndcgs).mean(), np.array(losses).mean()
                eval_time = time() - eval_begin

                if hr > best_hr:
                    best_hr = hr
                    best_ndcg = ndcg

                logging.info(
                    "Epoch %d [%.1fs + %.1fs]: HR = %.4f, NDCG = %.4f, loss = %.4f [%.1fs] train_loss = %.4f [%.1fs]"
                    % (epoch_count, batch_time, train_time, hr, ndcg,
                       test_loss, eval_time, train_loss, loss_time))
                print(
                    "Epoch %d [%.1fs + %.1fs]: HR = %.4f, NDCG = %.4f, loss = %.4f [%.1fs] train_loss = %.4f [%.1fs]"
                    % (epoch_count, batch_time, train_time, hr, ndcg,
                       test_loss, eval_time, train_loss, loss_time))

            batch_begin = time()
            batches = data.shuffle(dataset, model.batch_choice, num_negatives)
            np.random.shuffle(batch_index)
            batch_time = time() - batch_begin

        return best_hr, best_ndcg