test_recurrent_op.py 2.3 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
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):
    tensor = scope.create_var(name).get_tensor()
    tensor.set_dims(shape)
    tensor.alloc_float()
    tensor.set(np.random.random(shape))
    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
    '''

    def init(self):
        input_dim = 30
        batch_size = 50
        weight_dim = 15

        self.scope = core.Scope(None)

        # create vars
        create_tensor(self.scope, "x", [batch_size, input_dim])
        create_tensor(self.scope, "W", [input_dim, weight_dim])
        create_tensor(self.scope, "U", [weight_dim, weight_dim])
        create_tensor(self.scope, "h_boot", [batch_size, weight_dim])

        x_alias = "x@alias"
        y_alias = "y@alias"
        memory = "h@alias"
        prememory = "h@pre"
        output = "rnn_out"
        output_alias = "rnn_out@alias"

        # create step net
        stepnet_var = self.scope.create_var("stepnet")
        stepnet = stepnet_var.get_net()
        # stepnet = core.Net.create()
        x_fc_op = ops.fc(X=x_alias, W="W", Y="Wx")
        h_fc_op = ops.fc(X=prememory, W="U", Y="Uh")
        sum_op = ops.add_two(X="Wx", Y="Uh", Out="sum")
        sig_op = ops.sigmoid(X="sum", Y=memory)
        stepnet.add_op(x_fc_op)
        stepnet.add_op(h_fc_op)
        stepnet.add_op(sum_op)
        stepnet.add_op(sig_op)
        stepnet.complete_add_op(True)

        # create RNNOp
        rnnop = ops.recurrent_op(
            # inputs
            inlinks=["x"],
            boot_memories=["h_boot"],
            step_net="stepnet",
            # outputs
            outlinks=[output],
            step_scopes="step_scopes",
            # attributes
            inlink_alias=["x@alias"],
            outlink_alias=[output_alias],
            pre_memories=[prememory],
            memories=[memory])

        ctx = core.DeviceContext.cpu_context()
        rnnop.infer_shape(self.scope)
        rnnop.run(self.scope, ctx)

    def test_recurrent(self):
        self.init()


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