From 33590b583f46a889a5071b8185b1b987559e5021 Mon Sep 17 00:00:00 2001 From: JiabinYang Date: Wed, 23 Jan 2019 03:19:34 +0000 Subject: [PATCH] test=develop, move simple rnn cell to test_imperative --- python/paddle/fluid/imperative/nn.py | 93 +----------------- .../fluid/tests/unittests/test_imperative.py | 94 ++++++++++++++++++- .../unittests/test_imperative_ptb_rnn.py | 21 +++++ 3 files changed, 113 insertions(+), 95 deletions(-) create mode 100644 python/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py diff --git a/python/paddle/fluid/imperative/nn.py b/python/paddle/fluid/imperative/nn.py index d7d73df45fb..1bfeace521a 100644 --- a/python/paddle/fluid/imperative/nn.py +++ b/python/paddle/fluid/imperative/nn.py @@ -23,7 +23,7 @@ from ..framework import Variable, OpProtoHolder from ..param_attr import ParamAttr from ..initializer import Normal, Constant -__all__ = ['Conv2D', 'Pool2D', 'FC', 'SimpleRNNCell'] +__all__ = ['Conv2D', 'Pool2D', 'FC'] class Conv2D(layers.Layer): @@ -274,94 +274,3 @@ class FC(layers.Layer): out = bias_out # add activation return self._helper.append_activation(out) - - -class SimpleRNNCell(layers.Layer): - def __init__(self, step_input_size, hidden_size, output_size, param_attr): - super(SimpleRNNCell, self).__init__() - self.step_input_size = step_input_size - self.hidden_size = hidden_size - self.output_size = output_size - self._dype = core.VarDesc.VarType.FP32 - from ..layer_helper import LayerHelper - self._helper = LayerHelper( - 'SimpleRNNCell', act="tanh", param_attr=param_attr) - - def _build_once(self, inputs, pre_hidden): - i2h_param_shape = [self.step_input_size, self.hidden_size] - h2h_param_shape = [self.hidden_size, self.hidden_size] - h2o_param_shape = [self.output_size, self.hidden_size] - self._i2h_w = self._helper.create_parameter( - attr=self._helper.param_attr, - shape=i2h_param_shape, - dtype=self._dtype, - is_bias=False) - self._h2h_w = self._helper.create_parameter( - attr=self._helper.param_attr, - shape=h2h_param_shape, - dtype=self._dtype, - is_bias=False) - self._h2o_w = self._helper.create_parameter( - attr=self._helper.param_attr, - shape=h2o_param_shape, - dtype=self._dtype, - is_bias=False) - - def forward(self, input, pre_hidden): - - tmp_i2h = self._helper.create_variable_for_type_inference(self._dtype) - tmp_h2h = self._helper.create_variable_for_type_inference(self._dtype) - hidden = self._helper.create_variable_for_type_inference(self._dype) - out = self._helper.create_variable_for_type_inference(self._dype) - softmax_out = self._helper.create_variable_for_type_inference( - self._dtype) - reduce_out = self._helper.create_variable_for_type_inference( - self._dtype) - self._helper.append_op( - type="mul", - inputs={"X": input, - "Y": self._i2h_w}, - outputs={"Out": tmp_i2h}, - attrs={"x_num_col_dims": 1, - "y_num_col_dims": 1}) - - self._helper.append_op( - type="mul", - inputs={"X": pre_hidden, - "Y": self._h2h_w}, - outputs={"Out": tmp_h2h}, - attrs={"x_num_col_dims": 1, - "y_num_col_dims": 1}) - - self._helper.append_op( - type="elementwise_add", - inputs={'X': tmp_h2h, - 'Y': tmp_i2h}, - outputs={'Out': hidden}, - attrs={'axis': -1, - 'use_mkldnn': False}) - hidden = self._helper.append_activation(hidden) - - self._helper.append_op( - type="mul", - inputs={"X": hidden, - "Y": self._h2o_w}, - outputs={"Out": out}, - attrs={"x_num_col_dims": 1, - "y_num_col_dims": 1}) - - self._helper.append_op( - type="softmax", - inputs={"X": out}, - outputs={"Out": softmax_out}, - attrs={"use_cudnn": False}) - - self._helper.append_op( - type='reduce_sum', - inputs={'X': softmax_out}, - outputs={'Out': reduce_out}, - attrs={'dim': None, - 'keep_dim': False, - 'reduce_all': True}) - - return reduce_out, hidden diff --git a/python/paddle/fluid/tests/unittests/test_imperative.py b/python/paddle/fluid/tests/unittests/test_imperative.py index 0110a8dd476..07693caddb9 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative.py +++ b/python/paddle/fluid/tests/unittests/test_imperative.py @@ -20,9 +20,6 @@ import sys import paddle.fluid as fluid from paddle.fluid import core from paddle.fluid.imperative.nn import FC -from paddle.fluid.imperative.nn import SimpleRNNCell -from typing import List, Any, Tuple - from test_imperative_base import new_program_scope @@ -69,6 +66,97 @@ class MLP(fluid.imperative.Layer): return x +class SimpleRNNCell(fluid.imperative.Layer): + def __init__(self, step_input_size, hidden_size, output_size, param_attr): + super(SimpleRNNCell, self).__init__() + self.step_input_size = step_input_size + self.hidden_size = hidden_size + self.output_size = output_size + self._dype = core.VarDesc.VarType.FP32 + from paddle.fluid.layer_helper import LayerHelper + self._helper = LayerHelper( + 'SimpleRNNCell', act="tanh", param_attr=param_attr) + + def _build_once(self, inputs, pre_hidden): + i2h_param_shape = [self.step_input_size, self.hidden_size] + h2h_param_shape = [self.hidden_size, self.hidden_size] + h2o_param_shape = [self.output_size, self.hidden_size] + self._i2h_w = self._helper.create_parameter( + attr=self._helper.param_attr, + shape=i2h_param_shape, + dtype=self._dtype, + is_bias=False) + self._h2h_w = self._helper.create_parameter( + attr=self._helper.param_attr, + shape=h2h_param_shape, + dtype=self._dtype, + is_bias=False) + self._h2o_w = self._helper.create_parameter( + attr=self._helper.param_attr, + shape=h2o_param_shape, + dtype=self._dtype, + is_bias=False) + + def forward(self, input, pre_hidden): + + tmp_i2h = self._helper.create_variable_for_type_inference(self._dtype) + tmp_h2h = self._helper.create_variable_for_type_inference(self._dtype) + hidden = self._helper.create_variable_for_type_inference(self._dype) + out = self._helper.create_variable_for_type_inference(self._dype) + softmax_out = self._helper.create_variable_for_type_inference( + self._dtype) + reduce_out = self._helper.create_variable_for_type_inference( + self._dtype) + self._helper.append_op( + type="mul", + inputs={"X": input, + "Y": self._i2h_w}, + outputs={"Out": tmp_i2h}, + attrs={"x_num_col_dims": 1, + "y_num_col_dims": 1}) + + self._helper.append_op( + type="mul", + inputs={"X": pre_hidden, + "Y": self._h2h_w}, + outputs={"Out": tmp_h2h}, + attrs={"x_num_col_dims": 1, + "y_num_col_dims": 1}) + + self._helper.append_op( + type="elementwise_add", + inputs={'X': tmp_h2h, + 'Y': tmp_i2h}, + outputs={'Out': hidden}, + attrs={'axis': -1, + 'use_mkldnn': False}) + hidden = self._helper.append_activation(hidden) + + self._helper.append_op( + type="mul", + inputs={"X": hidden, + "Y": self._h2o_w}, + outputs={"Out": out}, + attrs={"x_num_col_dims": 1, + "y_num_col_dims": 1}) + + self._helper.append_op( + type="softmax", + inputs={"X": out}, + outputs={"Out": softmax_out}, + attrs={"use_cudnn": False}) + + self._helper.append_op( + type='reduce_sum', + inputs={'X': softmax_out}, + outputs={'Out': reduce_out}, + attrs={'dim': None, + 'keep_dim': False, + 'reduce_all': True}) + + return reduce_out, hidden + + class SimpleRNN(fluid.imperative.Layer): def __init__(self): super(SimpleRNN, self).__init__() diff --git a/python/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py b/python/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py new file mode 100644 index 00000000000..19df224770b --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py @@ -0,0 +1,21 @@ +# Copyright (c) 2018 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.framework as framework +import paddle.fluid.optimizer as optimizer +from paddle.fluid.backward import append_backward -- GitLab