test_mnist_if_else_op.py 5.1 KB
Newer Older
Y
Yu Yang 已提交
1
import paddle.v2.fluid.layers as layers
2
from paddle.v2.fluid.framework import Program, program_guard, default_main_program, default_startup_program
Y
Yu Yang 已提交
3 4 5 6 7 8 9 10 11 12
from paddle.v2.fluid.executor import Executor
from paddle.v2.fluid.optimizer import MomentumOptimizer
import paddle.v2.fluid.core as core
import paddle.v2 as paddle
import unittest
import numpy as np


class TestMNISTIfElseOp(unittest.TestCase):
    def test_raw_api(self):
13 14 15 16
        prog = Program()
        startup_prog = Program()
        with program_guard(prog, startup_prog):
            image = layers.data(name='x', shape=[784], dtype='float32')
Y
Yu Yang 已提交
17

18
            label = layers.data(name='y', shape=[1], dtype='int64')
Y
Yu Yang 已提交
19

20 21 22 23 24
            limit = layers.fill_constant_batch_size_like(
                input=label, dtype='int64', shape=[1], value=5.0)
            cond = layers.less_than(x=label, y=limit)
            true_image, false_image = layers.split_lod_tensor(
                input=image, mask=cond)
Y
Yu Yang 已提交
25

26 27
            true_out = layers.create_tensor(dtype='float32')
            true_cond = layers.ConditionalBlock([true_image])
Y
Yu Yang 已提交
28

29 30 31 32
            with true_cond.block():
                hidden = layers.fc(input=true_image, size=100, act='tanh')
                prob = layers.fc(input=hidden, size=10, act='softmax')
                layers.assign(input=prob, output=true_out)
Y
Yu Yang 已提交
33

34 35
            false_out = layers.create_tensor(dtype='float32')
            false_cond = layers.ConditionalBlock([false_image])
Y
Yu Yang 已提交
36

37 38 39 40
            with false_cond.block():
                hidden = layers.fc(input=false_image, size=200, act='tanh')
                prob = layers.fc(input=hidden, size=10, act='softmax')
                layers.assign(input=prob, output=false_out)
Y
Yu Yang 已提交
41

42 43 44 45
            prob = layers.merge_lod_tensor(
                in_true=true_out, in_false=false_out, mask=cond, x=image)
            loss = layers.cross_entropy(input=prob, label=label)
            avg_loss = layers.mean(x=loss)
Y
Yu Yang 已提交
46

47 48
            optimizer = MomentumOptimizer(learning_rate=0.001, momentum=0.9)
            optimizer.minimize(avg_loss, startup_prog)
Y
Yu Yang 已提交
49 50 51 52 53 54 55 56 57

        train_reader = paddle.batch(
            paddle.reader.shuffle(
                paddle.dataset.mnist.train(), buf_size=8192),
            batch_size=200)

        place = core.CPUPlace()
        exe = Executor(place)

58
        exe.run(startup_prog)
Y
Yu Yang 已提交
59 60 61 62 63 64 65
        PASS_NUM = 100
        for pass_id in range(PASS_NUM):
            for data in train_reader():
                x_data = np.array(map(lambda x: x[0], data)).astype("float32")
                y_data = np.array(map(lambda x: x[1], data)).astype("int64")
                y_data = np.expand_dims(y_data, axis=1)

66
                outs = exe.run(prog,
D
dzhwinter 已提交
67 68 69
                               feed={'x': x_data,
                                     'y': y_data},
                               fetch_list=[avg_loss])
Y
Yu Yang 已提交
70 71 72 73 74 75
                print outs[0]
                if outs[0] < 1.0:
                    return
        self.assertFalse(True)

    def test_ifelse(self):
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        prog = Program()
        startup_prog = Program()
        with program_guard(prog, startup_prog):
            image = layers.data(name='x', shape=[784], dtype='float32')

            label = layers.data(name='y', shape=[1], dtype='int64')

            limit = layers.fill_constant_batch_size_like(
                input=label, dtype='int64', shape=[1], value=5.0)
            cond = layers.less_than(x=label, y=limit)
            ie = layers.IfElse(cond)

            with ie.true_block():
                true_image = ie.input(image)
                hidden = layers.fc(input=true_image, size=100, act='tanh')
                prob = layers.fc(input=hidden, size=10, act='softmax')
                ie.output(prob)

            with ie.false_block():
                false_image = ie.input(image)
                hidden = layers.fc(input=false_image, size=200, act='tanh')
                prob = layers.fc(input=hidden, size=10, act='softmax')
                ie.output(prob)

            prob = ie()
            loss = layers.cross_entropy(input=prob[0], label=label)
            avg_loss = layers.mean(x=loss)

            optimizer = MomentumOptimizer(learning_rate=0.001, momentum=0.9)
            optimizer.minimize(avg_loss, startup_prog)
Y
Yu Yang 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119
        train_reader = paddle.batch(
            paddle.reader.shuffle(
                paddle.dataset.mnist.train(), buf_size=8192),
            batch_size=200)

        place = core.CPUPlace()
        exe = Executor(place)

        exe.run(kwargs['startup_program'])
        PASS_NUM = 100
        for pass_id in range(PASS_NUM):
            for data in train_reader():
                x_data = np.array(map(lambda x: x[0], data)).astype("float32")
                y_data = np.array(map(lambda x: x[1], data)).astype("int64")
D
dzhwinter 已提交
120
                y_data = y_data.reshape((y_data.shape[0], 1))
Y
Yu Yang 已提交
121

D
dzhwinter 已提交
122 123 124 125
                outs = exe.run(kwargs['main_program'],
                               feed={'x': x_data,
                                     'y': y_data},
                               fetch_list=[avg_loss])
Y
Yu Yang 已提交
126 127 128 129 130 131 132
                print outs[0]
                if outs[0] < 1.0:
                    return
        self.assertFalse(True)


if __name__ == '__main__':
133 134
    # temp disable if else unittest since it could be buggy.
    exit(0)