Example #1
0

# Load data
print("Loading data...")
trainset = Dataset('../../data/'+FLAGS.dataset+'/train.ss')
devset = Dataset('../../data/'+FLAGS.dataset+'/dev.ss')
testset = Dataset('../../data/'+FLAGS.dataset+'/test.ss')

alldata = np.concatenate([trainset.t_docs, devset.t_docs, testset.t_docs], axis=0)
embeddingpath = '../../data/'+FLAGS.dataset+'/embedding.txt'
embeddingfile, wordsdict = data_helpers.load_embedding(embeddingpath, alldata, FLAGS.embedding_dim)
del alldata
print("Loading data finished...")

usrdict, prddict = trainset.get_usr_prd_dict()
trainbatches = trainset.batch_iter(usrdict, prddict, wordsdict, FLAGS.n_class, FLAGS.batch_size,
                                 FLAGS.num_epochs, FLAGS.max_sen_len, FLAGS.max_doc_len)
devset.genBatch(usrdict, prddict, wordsdict, FLAGS.batch_size,
                  FLAGS.max_sen_len, FLAGS.max_doc_len, FLAGS.n_class)
testset.genBatch(usrdict, prddict, wordsdict, FLAGS.batch_size,
                  FLAGS.max_sen_len, FLAGS.max_doc_len, FLAGS.n_class)


with tf.Graph().as_default():
    session_config = tf.ConfigProto(
        allow_soft_placement=FLAGS.allow_soft_placement,
        log_device_placement=FLAGS.log_device_placement
    )
    session_config.gpu_options.allow_growth = True
    sess = tf.Session(config=session_config)
    with sess.as_default():
        huapataha = huapahata(
        topf1 = 0.
        toprmse = 0.
        better_dev_acc = 0.
        better_dev_rmse = 100
        predict_round = 0
        test_f1 = 0.0

        for ii in range(FLAGS.num_epochs):

            sum_len = len(trainset.t_label)
            counts = sum_len / 10000 + 1
            global_steps = 0
            for count in range(counts):
                trainbatches = trainset.batch_iter(count, wordsdict,
                                                   FLAGS.n_class,
                                                   FLAGS.batch_size, 1,
                                                   FLAGS.max_sen_len,
                                                   FLAGS.max_doc_len)
                # Training loop. For each batch...
                for tr_batch in trainbatches:
                    train_step(tr_batch)
                    current_step = tf.train.global_step(sess, global_step)
                    if current_step % FLAGS.evaluate_every == 0 or ii == FLAGS.num_epochs:
                        predict_round += 1
                        print("\nEvaluation round %d:" % (predict_round))

                        print "==========devset==========="
                        predict(devset, name="dev")
                        #print("dev_acc: %.4f    dev_RMSE: %.4f   dev_MAE: %.4f" % (dev_acc, dev_rmse,dev_mae))
                        test_f1 = predict(testset, name="test")
                        #print("test_acc: %.4f    test_RMSE: %.4f   test_MAE: %.4f" % (test_acc, test_rmse,test_mae)