Skip to content

Commit 86d7cc9

Browse files
Add bias for gru_unit_op and fix activation function (#10087)
1 parent 0d94ab1 commit 86d7cc9

File tree

1 file changed

+15
-18
lines changed
  • python/paddle/fluid/layers

1 file changed

+15
-18
lines changed

python/paddle/fluid/layers/nn.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,8 @@ def dynamic_gru(input,
699699
def gru_unit(input,
700700
hidden,
701701
size,
702-
weight=None,
703-
bias=None,
702+
param_attr=None,
703+
bias_attr=None,
704704
activation='tanh',
705705
gate_activation='sigmoid'):
706706
"""
@@ -731,8 +731,8 @@ def gru_unit(input,
731731
input (Variable): The fc transformed input value of current step.
732732
hidden (Variable): The hidden value of lstm unit from previous step.
733733
size (integer): The input dimension value.
734-
weight (ParamAttr): The weight parameters for gru unit. Default: None
735-
bias (ParamAttr): The bias parameters for gru unit. Default: None
734+
param_attr (ParamAttr): The weight parameters for gru unit. Default: None
735+
bias_attr (ParamAttr): The bias parameters for gru unit. Default: None
736736
activation (string): The activation type for cell (actNode).
737737
Default: 'tanh'
738738
gate_activation (string): The activation type for gates (actGate).
@@ -764,34 +764,31 @@ def gru_unit(input,
764764
size = size / 3
765765

766766
# create weight
767-
if weight is None:
768-
weight = helper.create_parameter(
769-
attr=helper.param_attr, shape=[size, 3 * size], dtype=dtype)
767+
weight = helper.create_parameter(
768+
attr=helper.param_attr, shape=[size, 3 * size], dtype=dtype)
770769

770+
gate = helper.create_tmp_variable(dtype)
771+
reset_hidden_pre = helper.create_tmp_variable(dtype)
772+
updated_hidden = helper.create_tmp_variable(dtype)
773+
inputs = {'Input': input, 'HiddenPrev': hidden, 'Weight': weight}
771774
# create bias
772-
773-
if bias is None:
775+
if helper.bias_attr:
774776
bias_size = [1, 3 * size]
775777
bias = helper.create_parameter(
776778
attr=helper.bias_attr, shape=bias_size, dtype=dtype, is_bias=True)
777-
778-
gate = helper.create_tmp_variable(dtype)
779-
reset_hidden_pre = helper.create_tmp_variable(dtype)
780-
updated_hidden = helper.create_tmp_variable(dtype)
779+
inputs['Bias'] = bias
781780

782781
helper.append_op(
783782
type='gru_unit',
784-
inputs={'Input': input,
785-
'HiddenPrev': hidden,
786-
'Weight': weight},
783+
inputs=inputs,
787784
outputs={
788785
'Gate': gate,
789786
'ResetHiddenPrev': reset_hidden_pre,
790787
'Hidden': updated_hidden,
791788
},
792789
attrs={
793-
'activation': 0,
794-
'gate_activation': 1,
790+
'activation': 2, # tanh
791+
'gate_activation': 1, # sigmoid
795792
})
796793

797794
return updated_hidden, reset_hidden_pre, gate

0 commit comments

Comments
 (0)