Add ParameterList (#21056)
* add ParameterList container * add unittest test=develop * fix container, rebase remove build_once test=develop * add parameters None check test=develop * update Sequential unittest test=develop * use Linear in Sequential sample code test=develop * fix Sequential arg type doc test=developrelease/1.7
parent
5cb2c74127
commit
cff7a49856
@ -0,0 +1,72 @@
|
||||
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# 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
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# 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.
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import unittest
|
||||
import paddle.fluid as fluid
|
||||
import numpy as np
|
||||
|
||||
|
||||
class MyLayer(fluid.Layer):
|
||||
def __init__(self, num_stacked_param):
|
||||
super(MyLayer, self).__init__()
|
||||
# create ParameterList with iterable Parameters
|
||||
self.params = fluid.dygraph.ParameterList(
|
||||
[fluid.layers.create_parameter(
|
||||
shape=[2, 2], dtype='float32')] * num_stacked_param)
|
||||
|
||||
def forward(self, x):
|
||||
for i, p in enumerate(self.params):
|
||||
tmp = self._helper.create_variable_for_type_inference('float32')
|
||||
self._helper.append_op(
|
||||
type="mul",
|
||||
inputs={"X": x,
|
||||
"Y": p},
|
||||
outputs={"Out": tmp},
|
||||
attrs={"x_num_col_dims": 1,
|
||||
"y_num_col_dims": 1})
|
||||
x = tmp
|
||||
return x
|
||||
|
||||
|
||||
class TestImperativeContainerParameterList(unittest.TestCase):
|
||||
def test_paramter_list(self):
|
||||
data_np = np.random.uniform(-1, 1, [5, 2]).astype('float32')
|
||||
with fluid.dygraph.guard():
|
||||
x = fluid.dygraph.to_variable(data_np)
|
||||
num_stacked_param = 4
|
||||
model = MyLayer(num_stacked_param)
|
||||
self.assertEqual(len(model.params), num_stacked_param)
|
||||
res = model(x)
|
||||
self.assertListEqual(res.shape, [5, 2])
|
||||
loss = fluid.layers.reduce_mean(res)
|
||||
loss.backward()
|
||||
|
||||
model.params[num_stacked_param - 1] = fluid.layers.create_parameter(
|
||||
shape=[2, 3], dtype='float32')
|
||||
res = model(x)
|
||||
self.assertListEqual(res.shape, [5, 3])
|
||||
model.params.append(
|
||||
fluid.layers.create_parameter(
|
||||
shape=[3, 4], dtype='float32'))
|
||||
self.assertEqual(len(model.params), num_stacked_param + 1)
|
||||
res = model(x)
|
||||
self.assertListEqual(res.shape, [5, 4])
|
||||
loss = fluid.layers.reduce_mean(res)
|
||||
loss.backward()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in new issue