提交 af1cee5a 编写于 作者: J JiabinYang

change in 1/18

上级 a360f143
...@@ -257,12 +257,14 @@ class SimpleRNNCell(layers.Layer): ...@@ -257,12 +257,14 @@ class SimpleRNNCell(layers.Layer):
output_size, output_size,
param_attr, param_attr,
dtype=core.VarDesc.VarType.FP32): dtype=core.VarDesc.VarType.FP32):
super(SimpleRNNCell, self).__init__()
self.input_size = step_input_size self.input_size = step_input_size
self.hidden_size = hidden_size self.hidden_size = hidden_size
self.output_size = output_size self.output_size = output_size
self._dype = core.VarDesc.VarType.FP32 self._dype = core.VarDesc.VarType.FP32
from ..layer_helper import LayerHelper 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): def _build_once(self, inputs):
i2h_param_shape = [self.step_input_size, self.hidden_size] i2h_param_shape = [self.step_input_size, self.hidden_size]
...@@ -284,20 +286,50 @@ class SimpleRNNCell(layers.Layer): ...@@ -284,20 +286,50 @@ class SimpleRNNCell(layers.Layer):
dtype=self._dtype, dtype=self._dtype,
is_bias=False) is_bias=False)
def forward(self, inputs): def forward(self, input, pre_hidden):
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)
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( self._helper.append_op(
type="mul", type="mul",
inputs={"X": input, 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}, outputs={"Out": out},
attrs={ attrs={"x_num_col_dims": 1,
"x_num_col_dims": self._num_flatten_dims, "y_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
...@@ -19,7 +19,7 @@ import sys ...@@ -19,7 +19,7 @@ import sys
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid import core 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 from test_imperative_base import new_program_scope
...@@ -70,9 +70,7 @@ class SimpleRNN(fluid.imperative.Layer): ...@@ -70,9 +70,7 @@ class SimpleRNN(fluid.imperative.Layer):
def __init__(self, inputs): def __init__(self, inputs):
super(SimpleRNN, self).__init__() super(SimpleRNN, self).__init__()
self.seq_len = input.shape[0] self.seq_len = input.shape[0]
self._fc1 = FC(3, self.cell = SimpleRNNCell(input.shape[1], out)
fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.1)))
def forward(self, inputs): def forward(self, inputs):
for i in range(self.seq_len): for i in range(self.seq_len):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册