test_fc_op.py 1.2 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8
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):
Y
Yu Yang 已提交
9
        scope = core.Scope()
Q
qijun 已提交
10
        place = core.CPUPlace()
Y
Yu Yang 已提交
11
        x = scope.new_var("X")
Q
qijun 已提交
12

Y
Yu Yang 已提交
13 14
        x_tensor = x.get_tensor()
        x_tensor.set_dims([1000, 784])
Q
qijun 已提交
15
        x_tensor.alloc_float(place)
Y
Yu Yang 已提交
16

Y
Yu Yang 已提交
17
        w = scope.new_var("W")
Y
Yu Yang 已提交
18 19
        w_tensor = w.get_tensor()
        w_tensor.set_dims([784, 100])
Q
qijun 已提交
20
        w_tensor.alloc_float(place)
Y
Yu Yang 已提交
21

Q
qijun 已提交
22
        w_tensor.set(numpy.random.random((784, 100)).astype("float32"), place)
Y
Yu Yang 已提交
23 24 25 26 27 28 29

        # 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():
Y
Yu Yang 已提交
30 31
            if scope.find_var(out) is None:
                scope.new_var(out).get_tensor()
Y
Yu Yang 已提交
32

Y
Yu Yang 已提交
33
        tensor = scope.find_var("Y").get_tensor()
Y
Yu Yang 已提交
34 35 36
        op.infer_shape(scope)
        self.assertEqual([1000, 100], tensor.shape())

Q
qijun 已提交
37
        ctx = core.DeviceContext.create(place)
Y
Yu Yang 已提交
38 39 40 41 42 43 44 45

        op.run(scope, ctx)

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


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