You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.8 KiB
50 lines
1.8 KiB
7 years ago
|
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||
7 years ago
|
#
|
||
7 years ago
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
# you may not use this file except in compliance with the License.
|
||
|
# You may obtain a copy of the License at
|
||
7 years ago
|
#
|
||
7 years ago
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
7 years ago
|
#
|
||
7 years ago
|
# Unless required by applicable law or agreed to in writing, software
|
||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
# See the License for the specific language governing permissions and
|
||
|
# limitations under the License.
|
||
|
|
||
7 years ago
|
import unittest
|
||
7 years ago
|
from paddle.v2.fluid.framework import default_main_program
|
||
7 years ago
|
import paddle.v2.fluid.core as core
|
||
7 years ago
|
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
|
||
7 years ago
|
|
||
7 years ago
|
main_program = default_main_program()
|
||
|
|
||
7 years ago
|
|
||
|
class TestParameter(unittest.TestCase):
|
||
|
def test_param(self):
|
||
7 years ago
|
shape = [784, 100]
|
||
|
val = 1.0625
|
||
7 years ago
|
b = main_program.global_block()
|
||
7 years ago
|
param = b.create_parameter(
|
||
|
name='fc.w',
|
||
7 years ago
|
shape=shape,
|
||
7 years ago
|
dtype='float32',
|
||
7 years ago
|
initializer=ConstantInitializer(val))
|
||
7 years ago
|
self.assertIsNotNone(param)
|
||
|
self.assertEqual('fc.w', param.name)
|
||
|
self.assertEqual((784, 100), param.shape)
|
||
7 years ago
|
self.assertEqual(core.VarDesc.VarType.FP32, param.dtype)
|
||
7 years ago
|
self.assertEqual(0, param.block.idx)
|
||
7 years ago
|
exe = Executor(core.CPUPlace())
|
||
7 years ago
|
p = exe.run(main_program, fetch_list=[param])[0]
|
||
7 years ago
|
self.assertTrue(np.allclose(p, np.ones(shape) * val))
|
||
7 years ago
|
p = io.get_parameter_value_by_name('fc.w', exe, main_program)
|
||
7 years ago
|
self.assertTrue(np.allclose(np.array(p), np.ones(shape) * val))
|
||
7 years ago
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
unittest.main()
|