test_parameter.py 1.2 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 23 24
        self.assertIsNotNone(param)
        self.assertEqual('fc.w', param.name)
        self.assertEqual((784, 100), param.shape)
        self.assertEqual(core.DataType.FP32, param.data_type)
        self.assertEqual(0, param.block.idx)
X
xuwei06 已提交
25 26 27 28 29
        exe = Executor(core.CPUPlace())
        p = exe.run(g_main_program, fetch_list=[param])[0]
        self.assertTrue(np.allclose(np.array(p), np.ones(shape) * val))
        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()