From af1cee5a3531093a035b74dca7b3dfdbce0c251b Mon Sep 17 00:00:00 2001 From: JiabinYang Date: Fri, 18 Jan 2019 09:22:02 +0000 Subject: [PATCH] change in 1/18 --- python/paddle/fluid/imperative/nn.py | 56 +++++++++++++++---- .../fluid/tests/unittests/test_imperative.py | 6 +- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/python/paddle/fluid/imperative/nn.py b/python/paddle/fluid/imperative/nn.py index 24f1865f3d1..bf735e8f1ad 100644 --- a/python/paddle/fluid/imperative/nn.py +++ b/python/paddle/fluid/imperative/nn.py @@ -257,12 +257,14 @@ class SimpleRNNCell(layers.Layer): output_size, param_attr, dtype=core.VarDesc.VarType.FP32): + super(SimpleRNNCell, self).__init__() self.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', param_attr=param_attr) + self._helper = LayerHelper( + 'SimpleRNNCell', act="tanh", param_attr=param_attr) def _build_once(self, inputs): i2h_param_shape = [self.step_input_size, self.hidden_size] @@ -284,20 +286,50 @@ class SimpleRNNCell(layers.Layer): dtype=self._dtype, is_bias=False) - def forward(self, inputs): - input = inputs[0] - pre_hidden = inputs[1] - out = self._helper.create_variable_for_type_inference(self._dtype) - hidden = self._helper.create_variable_for_type_inference(self._dype) + 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) self._helper.append_op( type="mul", inputs={"X": input, - "Y": self._w}, + "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='sum', + inputs={'X': [tmp_i2h, tmp_h2h]}, + outputs={'Out': hidden}, + attrs={'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": self._num_flatten_dims, - "y_num_col_dims": 1 - }) + 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}) - return 1 + return softmax_out, hidden diff --git a/python/paddle/fluid/tests/unittests/test_imperative.py b/python/paddle/fluid/tests/unittests/test_imperative.py index a578867a3df..3c9893bdda4 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative.py +++ b/python/paddle/fluid/tests/unittests/test_imperative.py @@ -19,7 +19,7 @@ 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 FC, SimpleRNNCell from test_imperative_base import new_program_scope @@ -70,9 +70,7 @@ class SimpleRNN(fluid.imperative.Layer): def __init__(self, inputs): super(SimpleRNN, self).__init__() self.seq_len = input.shape[0] - self._fc1 = FC(3, - fluid.ParamAttr( - initializer=fluid.initializer.Constant(value=0.1))) + self.cell = SimpleRNNCell(input.shape[1], out) def forward(self, inputs): for i in range(self.seq_len): -- GitLab