test_recurrent_op.py 4.7 KB
Newer Older
Y
Yan Chunwei 已提交
1
import logging
Y
Yan Chunwei 已提交
2 3 4
import paddle.v2.framework.core as core
import unittest
import numpy as np
S
superjom 已提交
5
from paddle.v2.framework.op import Operator
Y
Yan Chunwei 已提交
6

S
superjom 已提交
7 8

def py_sigmoid(x):
S
superjom 已提交
9
    return 1. / (1. + np.exp(-x))
S
superjom 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

class PySimpleRNN(object):
    '''
    A simple implementation of RNN based on numpy, to futhur test RecurrentOp's alogorithm
    '''
    def __init__(self,
                 input_dim = 30,
                 batch_size = 50,
                 weight_dim = 15,
                 sent_len = 11):
        self.x = np.random.normal(size=(sent_len, batch_size, input_dim))
        self.W = np.random.normal(size=(input_dim, input_dim))
        self.U = np.random.normal(size=(input_dim, input_dim))
        self.h_boot = np.random.normal(size=(batch_size, input_dim))

        # memories
        self.mems = [np.zeros(shape=(batch_size, input_dim)) for i in range(sent_len)]

    def forward(self):
        xs = self.segment_inputs()
        for step_id in range(self.x.shape[0]):
            self.step(step_id, xs[step_id])
        return self.concat_outputs()

    def segment_inputs(self):
        return [self.x[i] for i in range(self.x.shape[0])]

    def concat_outputs(self):
        return np.array(self.mems)

    def step(self, step_id, x):
        '''
        run a step
        '''
        mem = self.mems[step_id]
        if step_id > 0:
            pre_mem = self.mems[step_id-1]
        else:
            pre_mem = self.h_boot
        xW = np.matmul(x, self.W)
        hU = np.matmul(mem, self.U)

        sum = xW + hU
        self.mems[step_id] = py_sigmoid(sum)

class PySimpleRNNTest(unittest.TestCase):
    def setUp(self):
        self.rnn = PySimpleRNN()

    def test_forward(self):
        output = self.rnn.forward()
        print 'output', output
Y
Yan Chunwei 已提交
62 63


S
superjom 已提交
64
def create_tensor(scope, name, shape, np_data):
Y
Yan Chunwei 已提交
65
    tensor = scope.new_var(name).get_tensor()
Y
Yan Chunwei 已提交
66
    tensor.set_dims(shape)
S
superjom 已提交
67
    tensor.set(np_data, core.CPUPlace())
Y
Yan Chunwei 已提交
68 69 70
    return tensor


S
superjom 已提交
71
class TestRecurrentOp(unittest.TestCase):
Y
Yan Chunwei 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84
    '''
    Test RNNOp

    equation:
        h_t = \sigma (W x_t + U h_{t-1})
    weights:
        - W
        - U
    vars:
        - x
    memories:
        - h
    outputs:
S
superjom 已提交
85
       - h
Y
Yan Chunwei 已提交
86 87
    '''

Y
Yan Chunwei 已提交
88 89 90 91 92
    input_dim = 30
    batch_size = 50
    weight_dim = 15
    sent_len = 11

S
superjom 已提交
93 94 95 96 97
    def setUp(self):
        self.py_rnn = PySimpleRNN(self.input_dim,
                                  self.batch_size,
                                  self.weight_dim,
                                  self.sent_len)
Y
Yan Chunwei 已提交
98

Y
Yan Chunwei 已提交
99

S
superjom 已提交
100 101
    def forward(self):
        self.scope = core.Scope()
Y
Yan Chunwei 已提交
102 103 104 105 106 107
        self.create_global_variables()
        self.create_step_net()
        rnn_op = self.create_rnn_op()
        ctx = core.DeviceContext.create(core.CPUPlace())
        rnn_op.infer_shape(self.scope)
        rnn_op.run(self.scope, ctx)
S
superjom 已提交
108
        return np.array(self.scope.find_var("h").get_tensor())
Y
Yan Chunwei 已提交
109 110 111

    def create_global_variables(self):
        # create inlink
S
superjom 已提交
112
        x_np_data = self.py_rnn.x
Y
Yan Chunwei 已提交
113
        create_tensor(self.scope, "x",
S
superjom 已提交
114 115 116 117 118 119 120 121 122
                      [self.sent_len, self.batch_size, self.input_dim], x_np_data)
        W_np_data = self.py_rnn.W
        create_tensor(self.scope, "W", [self.input_dim, self.input_dim], W_np_data)

        U_np_data = self.py_rnn.U
        create_tensor(self.scope, "U", [self.input_dim, self.input_dim], U_np_data)

        h_boot_np_data = self.py_rnn.h_boot
        create_tensor(self.scope, "h_boot", [self.batch_size, self.input_dim], h_boot_np_data)
Y
Yan Chunwei 已提交
123 124 125 126 127
        self.scope.new_var("step_scopes")
        self.scope.new_var("h@alias")
        self.scope.new_var("h")

    def create_rnn_op(self):
Y
Yan Chunwei 已提交
128
        # create RNNOp
S
superjom 已提交
129
        rnnop = Operator("recurrent_op",
Y
Yan Chunwei 已提交
130 131 132 133 134
            # inputs
            inlinks=["x"],
            boot_memories=["h_boot"],
            step_net="stepnet",
            # outputs
Y
Yan Chunwei 已提交
135
            outlinks=["h"],
Y
Yan Chunwei 已提交
136 137 138
            step_scopes="step_scopes",
            # attributes
            inlink_alias=["x@alias"],
Y
Yan Chunwei 已提交
139 140 141 142 143 144 145 146
            outlink_alias=["h@alias"],
            pre_memories=["h@pre"],
            memories=["h@alias"])
        return rnnop

    def create_step_net(self):
        var = self.scope.new_var("stepnet")
        stepnet = var.get_net()
Y
Yan Chunwei 已提交
147

S
superjom 已提交
148 149 150 151
        x_fc_op = Operator("fc", X="x@alias", W="W", Y="Wx")
        h_fc_op = Operator("fc", X="h@pre", W="U", Y="Uh")
        sum_op = Operator("add_two", X="Wx", Y="Uh", Out="sum")
        sig_op = Operator("sigmoid", X="sum", Y="h@alias")
Y
Yan Chunwei 已提交
152 153 154 155

        for op in [x_fc_op, h_fc_op, sum_op, sig_op]:
            stepnet.add_op(op)
        stepnet.complete_add_op(True)
Y
Yan Chunwei 已提交
156

S
superjom 已提交
157 158
    def test_forward(self):
        print 'test recurrent op forward'
S
superjom 已提交
159 160 161 162 163 164
        pd_output = self.forward()
        py_output = self.py_rnn.forward()
        print 'pd_output', pd_output
        print
        print 'py_output', py_output
        self.assertEqual(pd_output.shape, py_output.shape)
Y
Yan Chunwei 已提交
165 166 167

if __name__ == '__main__':
    unittest.main()