test_parameter.py 1.1 KB
Newer Older
Y
Yu Yang 已提交
1
import unittest
Q
Qiao Longfei 已提交
2 3
from paddle.v2.fluid.framework import g_main_program
import paddle.v2.fluid.core as core
X
xuwei06 已提交
4 5 6 7
from paddle.v2.fluid.executor import Executor
import paddle.v2.fluid.io as io
from paddle.v2.fluid.initializer import ConstantInitializer
import numpy as np
Y
Yu Yang 已提交
8 9 10 11


class TestParameter(unittest.TestCase):
    def test_param(self):
X
xuwei06 已提交
12 13 14
        shape = [784, 100]
        val = 1.0625
        b = g_main_program.global_block()
Y
Yu Yang 已提交
15 16
        param = b.create_parameter(
            name='fc.w',
X
xuwei06 已提交
17
            shape=shape,
Y
Yu Yang 已提交
18
            dtype='float32',
X
xuwei06 已提交
19
            initializer=ConstantInitializer(val))
Y
Yu Yang 已提交
20 21 22
        self.assertIsNotNone(param)
        self.assertEqual('fc.w', param.name)
        self.assertEqual((784, 100), param.shape)
F
fengjiayi 已提交
23
        self.assertEqual(core.DataType.FP32, param.dtype)
Y
Yu Yang 已提交
24
        self.assertEqual(0, param.block.idx)
X
xuwei06 已提交
25 26
        exe = Executor(core.CPUPlace())
        p = exe.run(g_main_program, fetch_list=[param])[0]
D
dzhwinter 已提交
27
        self.assertTrue(np.allclose(p, np.ones(shape) * val))
X
xuwei06 已提交
28 29
        p = io.get_parameter_value_by_name('fc.w', exe, g_main_program)
        self.assertTrue(np.allclose(np.array(p), np.ones(shape) * val))
Y
Yu Yang 已提交
30 31 32 33


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