test_fc_op.py 1.1 KB
Newer Older
Y
Yu Yang 已提交
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
import paddle.v2.framework.core as core
import unittest
import numpy
import paddle.v2.framework.create_op_creation_methods as creation


class TestFc(unittest.TestCase):
    def test_fc(self):
        scope = core.Scope(None)
        x = scope.create_var("X")
        x_tensor = x.get_tensor()
        x_tensor.set_dims([1000, 784])
        x_tensor.alloc_float()

        w = scope.create_var("W")
        w_tensor = w.get_tensor()
        w_tensor.set_dims([784, 100])
        w_tensor.alloc_float()

        w_tensor.set(numpy.random.random((784, 100)).astype("float32"))

        # Set a real numpy array here.
        # x_tensor.set(numpy.array([]))

        op = creation.op_creations.fc(X="X", Y="Y", W="W")

        for out in op.outputs():
            if scope.get_var(out) is None:
                scope.create_var(out).get_tensor()

        tensor = scope.get_var("Y").get_tensor()
        op.infer_shape(scope)
        self.assertEqual([1000, 100], tensor.shape())

        ctx = core.DeviceContext.cpu_context()

        op.run(scope, ctx)

        # After complete all ops, check Y is expect or not.


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