Exemplo n.º 1
0
def test_batch_flow_bucket():
    from fake_data import generate
    x_data, y_data, ws_input, ws_target = generate(size=10000)
    flow = batch_flow_bucket([x_data, y_data], [ws_input, ws_target], 4, debug=True)
    for _ in range(10):
        x, xl, y, yl = next(flow)
        print(x.shape, y.shape, xl.shape, yl.shape)
Exemplo n.º 2
0
def test_batch_flow():
    """test batch_flow function"""
    from fake_data import generate
    x_data, y_data, ws_input, ws_target = generate(size=10000)
    flow = batch_flow([x_data, y_data], [ws_input, ws_target], 4)
    x, xl, y, yl = next(flow)
    print(x.shape, y.shape, xl.shape, yl.shape)
def test():
    """单元测试"""
    from fake_data import generate
    from data_utils import batch_flow
    from tqdm import tqdm

    x_data, y_data, ws_input, ws_target = generate(size=10000)

    batch_size = 4
    n_epoch = 10
    steps = int(len(x_data) / batch_size) + 1

    config = tf.ConfigProto(device_count={
        'CPU': 1,
        'GPU': 0
    },
                            allow_soft_placement=True,
                            log_device_placement=False)

    tf.reset_default_graph()
    with tf.Graph().as_default():
        random.seed(0)
        np.random.seed(0)
        tf.set_random_seed(0)

        with tf.Session(config=config) as sess:

            model = Discriminative(len(ws_input), batch_size=batch_size)
            init = tf.global_variables_initializer()
            sess.run(init)

            # print(sess.run(model.input_layer.kernel))
            # exit(1)

            for epoch in range(1, n_epoch + 1):
                costs = []
                flow = batch_flow([x_data, y_data], [ws_input, ws_target],
                                  batch_size)
                bar = tqdm(range(steps),
                           desc='epoch {}, loss=0.000000'.format(epoch))
                for _ in bar:
                    x, xl, y, yl = next(flow)
                    targets = np.array([(0, 1) for x in range(len(y))])
                    cost = model.train(sess, x, xl, y, yl, targets)
                    print(x.shape, xl.shape)
                    print('cost.shape, cost', cost.shape, cost)
                    exit(1)
                    costs.append(cost)
                    bar.set_description('epoch {} loss={:.6f}'.format(
                        epoch, np.mean(costs)))
Exemplo n.º 4
0
def test(bidirectional, cell_type, depth,
         attention_type, use_residual, use_dropout, time_major):
    """测试不同参数在生成的假数据上的运行结果"""

    # 获取一些假数据
    x_data, y_data, ws_input, ws_target = generate(size=10000)

    # 训练部分

    split = int(len(x_data) * 0.8)
    x_train, x_test, y_train, y_test = (
        x_data[:split], x_data[split:], y_data[:split], y_data[split:])
    n_epoch = 2
    batch_size = 32
    steps = int(len(x_train) / batch_size) + 1

    config = tf.ConfigProto(
        device_count={'CPU': 1, 'GPU': 0},
        allow_soft_placement=True,
        log_device_placement=False
    )

    save_path = '/tmp/s2ss.ckpt'

    tf.reset_default_graph()
    with tf.Graph().as_default():
        random.seed(0)
        np.random.seed(0)
        tf.set_random_seed(0)

        with tf.Session(config=config) as sess:

            model = SequenceToSequence(
                input_vocab_size=len(ws_input),
                target_vocab_size=len(ws_target),
                batch_size=batch_size,
                learning_rate=0.001,
                bidirectional=bidirectional,
                cell_type=cell_type,
                depth=depth,
                attention_type=attention_type,
                use_residual=use_residual,
                use_dropout=use_dropout,
                time_major=time_major,
                hidden_units=64,
                embedding_size=64,
                parallel_iterations=1 # for test
            )
            init = tf.global_variables_initializer()
            sess.run(init)

            # print(sess.run(model.input_layer.kernel))
            # exit(1)

            for epoch in range(1, n_epoch + 1):
                costs = []
                flow = batch_flow(
                    [x_train, y_train], [ws_input, ws_target], batch_size
                )
                bar = tqdm(range(steps),
                           desc='epoch {}, loss=0.000000'.format(epoch))
                for _ in bar:
                    x, xl, y, yl = next(flow)
                    cost = model.train(sess, x, xl, y, yl)
                    costs.append(cost)
                    bar.set_description('epoch {} loss={:.6f}'.format(
                        epoch,
                        np.mean(costs)
                    ))

            model.save(sess, save_path)

    # 测试部分
    tf.reset_default_graph()
    model_pred = SequenceToSequence(
        input_vocab_size=len(ws_input),
        target_vocab_size=len(ws_target),
        batch_size=1,
        mode='decode',
        beam_width=5,
        bidirectional=bidirectional,
        cell_type=cell_type,
        depth=depth,
        attention_type=attention_type,
        use_residual=use_residual,
        use_dropout=use_dropout,
        time_major=time_major,
        hidden_units=64,
        embedding_size=64,
        parallel_iterations=1 # for test
    )
    init = tf.global_variables_initializer()

    with tf.Session(config=config) as sess:
        sess.run(init)
        model_pred.load(sess, save_path)

        bar = batch_flow([x_test, y_test], [ws_input, ws_target], 1)
        t = 0
        for x, xl, y, yl in bar:
            pred = model_pred.predict(
                sess,
                np.array(x),
                np.array(xl)
            )
            print(ws_input.inverse_transform(x[0]))
            print(ws_target.inverse_transform(y[0]))
            print(ws_target.inverse_transform(pred[0]))
            t += 1
            if t >= 3:
                break

    tf.reset_default_graph()
    model_pred = SequenceToSequence(
        input_vocab_size=len(ws_input),
        target_vocab_size=len(ws_target),
        batch_size=1,
        mode='decode',
        beam_width=0,
        bidirectional=bidirectional,
        cell_type=cell_type,
        depth=depth,
        attention_type=attention_type,
        use_residual=use_residual,
        use_dropout=use_dropout,
        time_major=time_major,
        hidden_units=64,
        embedding_size=64,
        parallel_iterations=1 # for test
    )
    init = tf.global_variables_initializer()

    with tf.Session(config=config) as sess:
        sess.run(init)
        model_pred.load(sess, save_path)

        bar = batch_flow([x_test, y_test], [ws_input, ws_target], 1)
        t = 0
        for x, xl, y, yl in bar:
            pred = model_pred.predict(
                sess,
                np.array(x),
                np.array(xl)
            )
            print(ws_input.inverse_transform(x[0]))
            print(ws_target.inverse_transform(y[0]))
            print(ws_target.inverse_transform(pred[0]))
            t += 1
            if t >= 3:
                break

    # return last train loss
    return np.mean(costs)
Exemplo n.º 5
0
def test(bidirectional, cell_type, depth, attention_type):
    """测试并展示attention图
    """

    from tqdm import tqdm
    from fake_data import generate

    # 获取一些假数据
    x_data, y_data, ws_input, ws_target = generate(size=10000)

    # 训练部分

    split = int(len(x_data) * 0.9)
    x_train, x_test, y_train, y_test = (x_data[:split], x_data[split:],
                                        y_data[:split], y_data[split:])
    n_epoch = 2
    batch_size = 32
    steps = int(len(x_train) / batch_size) + 1

    config = tf.ConfigProto(device_count={
        'CPU': 1,
        'GPU': 0
    },
                            allow_soft_placement=True,
                            log_device_placement=False)

    save_path = '/tmp/s2ss_atten.ckpt'

    with tf.Graph().as_default():

        model = SequenceToSequence(input_vocab_size=len(ws_input),
                                   target_vocab_size=len(ws_target),
                                   batch_size=batch_size,
                                   learning_rate=0.001,
                                   bidirectional=bidirectional,
                                   cell_type=cell_type,
                                   depth=depth,
                                   attention_type=attention_type,
                                   parallel_iterations=1)
        init = tf.global_variables_initializer()

        with tf.Session(config=config) as sess:
            sess.run(init)
            for epoch in range(1, n_epoch + 1):
                costs = []
                flow = batch_flow([x_train, y_train], [ws_input, ws_target],
                                  batch_size)
                bar = tqdm(range(steps),
                           desc='epoch {}, loss=0.000000'.format(epoch))
                for _ in bar:
                    x, xl, y, yl = next(flow)
                    cost = model.train(sess, x, xl, y, yl)
                    costs.append(cost)
                    bar.set_description('epoch {} loss={:.6f}'.format(
                        epoch, np.mean(costs)))

            model.save(sess, save_path)

    # attention 展示 不能用 beam search 的
    # 所以这里只是用 greedy

    with tf.Graph().as_default():
        model_pred = SequenceToSequence(input_vocab_size=len(ws_input),
                                        target_vocab_size=len(ws_target),
                                        batch_size=1,
                                        mode='decode',
                                        beam_width=0,
                                        bidirectional=bidirectional,
                                        cell_type=cell_type,
                                        depth=depth,
                                        attention_type=attention_type,
                                        parallel_iterations=1)
        init = tf.global_variables_initializer()

        with tf.Session(config=config) as sess:
            sess.run(init)
            model_pred.load(sess, save_path)

            pbar = batch_flow([x_test, y_test], [ws_input, ws_target], 1)
            t = 0
            for x, xl, y, yl in pbar:
                pred, atten = model_pred.predict(sess,
                                                 np.array(x),
                                                 np.array(xl),
                                                 attention=True)
                ox = ws_input.inverse_transform(x[0])
                oy = ws_target.inverse_transform(y[0])
                op = ws_target.inverse_transform(pred[0])
                print(ox)
                print(oy)
                print(op)

                fig, ax = plt.subplots()
                cax = ax.matshow(atten.reshape(
                    [atten.shape[0], atten.shape[2]]),
                                 cmap=cm.coolwarm)
                ax.set_xticks(np.arange(len(ox)))
                ax.set_yticks(np.arange(len(op)))
                ax.set_xticklabels(ox)
                ax.set_yticklabels(op)
                fig.colorbar(cax)
                plt.show()

                print('-' * 30)

                t += 1
                if t >= 10:
                    break
Exemplo n.º 6
0
def test_batch_flow():
    from fake_data import generate
    x_data, y_data, ws_input, ws_target = generate(size=10000)
    flow = batch_flow([x_data, y_data], [ws_input, ws_target], 4)
    x, x1, y, y1 = next(flow)
    print(x.shape, y.shape, x1.shape, y1.shape)
Exemplo n.º 7
0
def test(bidirectional, cell_type, depth, use_residual, use_dropout,
         output_project_active, crf_loss):
    """测试不同参数在生成的假数据上的运行结果"""

    # 获取一些假数据
    x_data, y_data, ws_input, ws_target = generate(size=10000, same_len=True)

    # 训练部分

    split = int(len(x_data) * 0.8)
    x_train, x_test, y_train, y_test = (x_data[:split], x_data[split:],
                                        y_data[:split], y_data[split:])
    n_epoch = 1
    batch_size = 32
    steps = int(len(x_train) / batch_size) + 1

    config = tf.ConfigProto(device_count={
        'CPU': 1,
        'GPU': 0
    },
                            allow_soft_placement=True,
                            log_device_placement=False)

    save_path = '/tmp/s2ss_crf.ckpt'

    tf.reset_default_graph()
    with tf.Graph().as_default():
        random.seed(0)
        np.random.seed(0)
        tf.set_random_seed(0)

        with tf.Session(config=config) as sess:

            model = RNNCRF(input_vocab_size=len(ws_input),
                           target_vocab_size=len(ws_target),
                           max_decode_step=100,
                           batch_size=batch_size,
                           learning_rate=0.001,
                           bidirectional=bidirectional,
                           cell_type=cell_type,
                           depth=depth,
                           use_residual=use_residual,
                           use_dropout=use_dropout,
                           output_project_active=output_project_active,
                           hidden_units=64,
                           embedding_size=64,
                           parallel_iterations=1,
                           crf_loss=crf_loss)
            init = tf.global_variables_initializer()
            sess.run(init)

            # print(sess.run(model.input_layer.kernel))
            # exit(1)

            for epoch in range(1, n_epoch + 1):
                costs = []
                flow = batch_flow([x_train, y_train], [ws_input, ws_target],
                                  batch_size)
                bar = tqdm(range(steps),
                           desc='epoch {}, loss=0.000000'.format(epoch))
                for _ in bar:
                    x, xl, y, yl = next(flow)
                    cost = model.train(sess, x, xl, y, yl)
                    costs.append(cost)
                    bar.set_description('epoch {} loss={:.6f}'.format(
                        epoch, np.mean(costs)))

            model.save(sess, save_path)

    # 测试部分
    tf.reset_default_graph()
    model_pred = RNNCRF(input_vocab_size=len(ws_input),
                        target_vocab_size=len(ws_target),
                        max_decode_step=100,
                        batch_size=batch_size,
                        mode='decode',
                        bidirectional=bidirectional,
                        cell_type=cell_type,
                        depth=depth,
                        use_residual=use_residual,
                        use_dropout=use_dropout,
                        output_project_active=output_project_active,
                        hidden_units=64,
                        embedding_size=64,
                        parallel_iterations=1,
                        crf_loss=crf_loss)
    init = tf.global_variables_initializer()

    with tf.Session(config=config) as sess:
        sess.run(init)
        model_pred.load(sess, save_path)

        flow = batch_flow([x_test, y_test], [ws_input, ws_target], batch_size)
        pbar = tqdm(range(100))
        acc = []
        for i in pbar:
            x, xl, y, yl = next(flow)
            pred = model_pred.predict(sess, np.array(x), np.array(xl))

            for j in range(batch_size):
                right = np.sum(y[j][:yl[j]] == pred[j][:yl[j]])
                acc.append(right / yl[j])

            if i < 3:
                print(ws_input.inverse_transform(x[0]))
                print(ws_target.inverse_transform(y[0]))
                print(ws_target.inverse_transform(pred[0]))
            else:
                pbar.set_description('acc: {}'.format(np.mean(acc)))