test_recurrent_op.py 2.5 KB
Newer Older
Y
Yan Chunwei 已提交
1
import logging
Y
Yan Chunwei 已提交
2 3 4 5 6 7 8 9 10
import paddle.v2.framework.core as core
import unittest
import numpy as np
import paddle.v2.framework.create_op_creation_methods as creation

ops = creation.op_creations


def create_tensor(scope, name, shape):
Y
Yan Chunwei 已提交
11
    tensor = scope.new_var(name).get_tensor()
Y
Yan Chunwei 已提交
12
    tensor.set_dims(shape)
Y
Yan Chunwei 已提交
13
    tensor.set(np.random.random(shape), core.CPUPlace())
Y
Yan Chunwei 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    return tensor


class TestRNN(unittest.TestCase):
    '''
    Test RNNOp

    equation:
        h_t = \sigma (W x_t + U h_{t-1})
    weights:
        - W
        - U
    vars:
        - x
    memories:
        - h
    outputs:
        - h
    '''

Y
Yan Chunwei 已提交
34 35 36 37 38
    input_dim = 30
    batch_size = 50
    weight_dim = 15
    sent_len = 11

Y
Yan Chunwei 已提交
39 40
    def init(self):

Y
Yan Chunwei 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        self.scope = core.Scope()

        self.create_global_variables()
        self.create_step_net()
        rnn_op = self.create_rnn_op()
        ctx = core.DeviceContext.create(core.CPUPlace())
        print 'infer_shape'
        rnn_op.infer_shape(self.scope)

        rnn_op.run(self.scope, ctx)

    def create_global_variables(self):
        # create inlink
        create_tensor(self.scope, "x",
                      [self.sent_len, self.batch_size, self.input_dim])
        create_tensor(self.scope, "W", [self.input_dim, self.input_dim])
        create_tensor(self.scope, "U", [self.input_dim, self.input_dim])
        create_tensor(self.scope, "h_boot", [self.batch_size, self.input_dim])
        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 已提交
64 65 66 67 68 69 70
        # create RNNOp
        rnnop = ops.recurrent_op(
            # inputs
            inlinks=["x"],
            boot_memories=["h_boot"],
            step_net="stepnet",
            # outputs
Y
Yan Chunwei 已提交
71
            outlinks=["h"],
Y
Yan Chunwei 已提交
72 73 74
            step_scopes="step_scopes",
            # attributes
            inlink_alias=["x@alias"],
Y
Yan Chunwei 已提交
75 76 77 78 79 80 81 82
            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 已提交
83

Y
Yan Chunwei 已提交
84 85 86 87 88 89 90 91
        x_fc_op = ops.fc(X="x@alias", W="W", Y="Wx")
        h_fc_op = ops.fc(X="h@pre", W="U", Y="Uh")
        sum_op = ops.add_two(X="Wx", Y="Uh", Out="sum")
        sig_op = ops.sigmoid(X="sum", Y="h@alias")

        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 已提交
92 93 94 95 96 97 98

    def test_recurrent(self):
        self.init()


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