Esempio n. 1
0
        #     _true = np.concatenate((_true, y_batch))
        _accs += _acc
        _costs += _cost
    mean_acc = _accs / batch_num
    mean_cost = _costs / batch_num
    cm = confusion_matrix(y_true=_true, y_pred=_pred)
    return mean_acc, mean_cost, cm


if __name__ == '__main__':

    max_len = 150
    # data_train, data_valid, data_test = data_helper.getDataSet()
    data_train, data_test = data_helper.getDataSet()
    # wordEmbedding = data_helper.getWordEmbedding(targetFileName='./data/embeddingMatrix.300d.pkl')
    wordEmbedding = data_helper.getRandomWordEmbedding(vocabularySize=10002,
                                                       embeddingSize=300)

    # model = TextBiLSTM(wordEmbedding, textLength=max_len, vocabulary_size=10002, embedding_size=128)
    model = TextBiLSTM(wordEmbedding,
                       textLength=max_len,
                       vocabulary_size=27590,
                       embedding_size=300)
    # model = TextBiLSTM(wordEmbedding, textLength=max_len, vocabulary_size=42488, embedding_size=128)

    config = tf.ConfigProto()
    sess = tf.Session(config=config)
    init = tf.global_variables_initializer()
    sess.run(init)

    model_save_path = './ckpt/bilstmModel/bilstm.ckpt'
    # tr_batch_size = 128
Esempio n. 2
0
def TrainAndTest():
    max_len = 150
    data_train, data_valid, data_test = data_helper.getDataSet()
    # wordEmbedding = data_helper.getWordEmbedding(targetFileName='./data/embeddingMatrix.300d.pkl')
    wordEmbedding = data_helper.getRandomWordEmbedding(vocabularySize=10002,
                                                       embeddingSize=300)

    model = BiLSTMForMultiTask(wordEmbedding,
                               textLength=max_len,
                               embedding_size=300)

    config = tf.ConfigProto()
    sess = tf.Session(config=config)
    init = tf.global_variables_initializer()
    sess.run(init)

    model_save_path = './ckpt/bilstmForMulitTask/bilstm.ckpt'
    tr_batch_size = 128
    decay = 0.85
    max_epoch = 5
    max_max_epoch = 12
    # max_epoch = 12
    # max_max_epoch = 25
    display_num = 5
    tr_batch_num = int(data_train.y.shape[0] / tr_batch_size)
    display_batch = int(tr_batch_num / display_num)

    for epoch in xrange(max_max_epoch):
        _lr = 1e-3
        if epoch > max_epoch:
            _lr = _lr * ((decay)**(epoch - max_epoch))
        print 'EPOCH {}, lr={}'.format(epoch + 1, _lr)

        start_time = time.time()
        _accs = np.zeros(shape=[4], dtype=np.float32)
        _costs = 0.0
        show_accs = np.zeros(shape=[4], dtype=np.float32)
        show_costs = 0.0

        for batch in xrange(tr_batch_num):
            fetches = [model.accuracy, model.cost, model.train_op]
            X_q_batch, X_r_batch, y_batch = data_train.next_batch(
                tr_batch_size)
            feed_dict = {
                model.X_q_inputs: X_q_batch,
                model.X_r_inputs: X_r_batch,
                model.y_inputs: y_batch,
                model.batch_size: tr_batch_size,
                model.dropout_keep_prob: 0.5,
                model.lr: _lr,
            }

            _acc, _cost, _ = sess.run(fetches, feed_dict)
            _accs += _acc
            _costs += _cost
            show_accs += _acc
            show_costs += _cost
            if (batch + 1) % display_batch == 0:
                valid_acc, valid_cost = test_epoch(sess, model, data_valid)

                disagree_agree_acc = show_accs[0] / display_batch
                attacking_respectful_acc = show_accs[1] / display_batch
                emotion_fact_acc = show_accs[2] / display_batch
                nasty_nice_acc = show_accs[3] / display_batch

                print '\ttraining acc={}, {}, {}, {}, cost={};  valid acc={}, {}, {}, {}, cost={} '.format(
                    disagree_agree_acc, attacking_respectful_acc,
                    emotion_fact_acc, nasty_nice_acc,
                    show_costs / display_batch, valid_acc[0], valid_acc[1],
                    valid_acc[2], valid_acc[3], valid_cost)

                show_accs = np.zeros(shape=[4], dtype=np.float32)
                show_costs = 0.0

        mean_acc = _accs / tr_batch_num
        mean_cost = _costs / tr_batch_num

        if (epoch + 1) % 3 == 0:
            save_path = model.saver.save(sess,
                                         model_save_path,
                                         global_step=(epoch + 1))
            print 'The save path is ', save_path
        print '\ttraining {}, acc={}, {}, {}, {}, cost={}'.format(
            data_train.y.shape[0], mean_acc[0], mean_acc[1], mean_acc[2],
            mean_acc[3], mean_cost)
        print 'Epoch training {}, acc={}, {}, {}, {}, cost={}, speed={} s/epoch\n'.format(
            data_train.y.shape[0], mean_acc[0], mean_acc[1], mean_acc[2],
            mean_acc[3], mean_cost,
            time.time() - start_time)
    print '**TEST RESULT:'
    test_acc, test_cost = test_epoch(sess, model, data_test)
    print '**Test {}, acc={}, {}, {}, {}, cost={}'.format(
        data_test.y.shape[0], test_acc[0], test_acc[1], test_acc[2],
        test_acc[3], test_cost)