Skip to content

Commit 8e957df

Browse files
luotao1emailweixu
authored andcommitted
fix bug in dotmul_operator's api and anotation (#99)
* fix bug in dotmul_operator's api and anotation * update rnn document * remove redundant info of projection and operator in layers.py
1 parent 98bc889 commit 8e957df

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

doc/algorithm/rnn/rnn.rst

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,15 @@ We also project the encoder vector to :code:`decoder_size` dimensional space, ge
142142
The decoder uses :code:`recurrent_group` to define the recurrent neural network. The step and output functions are defined in :code:`gru_decoder_with_attention`:
143143

144144
.. code-block:: python
145-
145+
group_inputs=[StaticInput(input=encoded_vector,is_seq=True),
146+
StaticInput(input=encoded_proj,is_seq=True)]
146147
trg_embedding = embedding_layer(
147148
input=data_layer(name='target_language_word',
148149
size=target_dict_dim),
149150
size=word_vector_dim,
150151
param_attr=ParamAttr(name='_target_language_embedding'))
152+
group_inputs.append(trg_embedding)
153+
151154
# For decoder equipped with attention mechanism, in training,
152155
# target embedding (the groudtruth) is the data input,
153156
# while encoded source sequence is accessed to as an unbounded memory.
@@ -156,13 +159,7 @@ The decoder uses :code:`recurrent_group` to define the recurrent neural network.
156159
# All sequence inputs should have the same length.
157160
decoder = recurrent_group(name=decoder_group_name,
158161
step=gru_decoder_with_attention,
159-
input=[
160-
StaticInput(input=encoded_vector,
161-
is_seq=True),
162-
StaticInput(input=encoded_proj,
163-
is_seq=True),
164-
trg_embedding
165-
])
162+
input=group_inputs)
166163
167164
168165
The implementation of the step function is listed as below. First, it defines the **memory** of the decoder network. Then it defines attention, gated recurrent unit step function, and the output function:
@@ -217,10 +214,8 @@ The code is listed below:
217214

218215
.. code-block:: python
219216
220-
gen_inputs = [StaticInput(input=encoded_vector,
221-
is_seq=True),
222-
StaticInput(input=encoded_proj,
223-
is_seq=True), ]
217+
group_inputs=[StaticInput(input=encoded_vector,is_seq=True),
218+
StaticInput(input=encoded_proj,is_seq=True)]
224219
# In generation, decoder predicts a next target word based on
225220
# the encoded source sequence and the last generated target word.
226221
# The encoded source sequence (encoder's output) must be specified by
@@ -231,10 +226,10 @@ The code is listed below:
231226
size=target_dict_dim,
232227
embedding_name='_target_language_embedding',
233228
embedding_size=word_vector_dim)
234-
gen_inputs.append(trg_embedding)
229+
group_inputs.append(trg_embedding)
235230
beam_gen = beam_search(name=decoder_group_name,
236231
step=gru_decoder_with_attention,
237-
input=gen_inputs,
232+
input=group_inputs,
238233
id_input=data_layer(name="sent_id",
239234
size=1),
240235
dict_file=trg_dict_path,

doc/ui/api/trainer_config_helpers/layers.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ dotmul_projection
169169
:members: dotmul_projection
170170
:noindex:
171171

172+
dotmul_operator
173+
---------------
174+
.. automodule:: paddle.trainer_config_helpers.layers
175+
:members: dotmul_operator
176+
:noindex:
177+
172178
full_matrix_projection
173179
----------------------
174180
.. automodule:: paddle.trainer_config_helpers.layers

python/paddle/trainer/config_parser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,11 +2464,11 @@ def __init__(
24642464
if size != 0:
24652465
self.set_layer_size(size)
24662466
else:
2467-
size = operator.calc_output_size(operator_conf.input_sizes)
2468-
if size != 0:
2469-
config_assert(size == self.config.size,
2467+
sz = operator.calc_output_size(operator_conf.input_sizes)
2468+
if sz != 0:
2469+
config_assert(sz == self.config.size,
24702470
"different inputs have different size: %s vs. %s" %
2471-
(size, self.config.size))
2471+
(sz, self.config.size))
24722472
for input_index in xrange(len(self.inputs)):
24732473
input_layer = self.get_input_layer(input_index)
24742474
input = self.inputs[input_index]

python/paddle/trainer_config_helpers/layers.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ def full_matrix_projection(input, size=0, param_attr=None):
286286
size=size,
287287
**param_attr.attr)
288288
proj.origin = input
289-
proj.origin.projection = "matrix"
290289
return proj
291290

292291

@@ -333,7 +332,6 @@ def table_projection(input, size=0, param_attr=None):
333332
size=size,
334333
**param_attr.attr)
335334
proj.origin = input
336-
proj.origin.projection = "table"
337335
return proj
338336

339337

@@ -377,17 +375,15 @@ def identity_projection(input, offset=None):
377375
if offset is None:
378376
proj = IdentityProjection(input_layer_name=input.name)
379377
proj.origin = input
380-
proj.origin.projection = 'identity'
381378
else:
382379
proj = IdentityOffsetProjection(input_layer_name=input.name,
383380
offset=offset)
384381
proj.origin = input
385-
proj.origin.projection = 'identity_offset'
386382
return proj
387383

388384

389385
@wrap_param_attr_default()
390-
def dotmul_projection(input, param_attr=None, scale=1):
386+
def dotmul_projection(input, param_attr=None):
391387
"""
392388
DotMulProjection with a layer as input.
393389
It performs element-wise multiplication with weight.
@@ -407,30 +403,35 @@ def dotmul_projection(input, param_attr=None, scale=1):
407403
:type input: LayerOutput
408404
:param param_attr: Parameter config, None if use default.
409405
:type param_attr: ParameterAttribute
410-
:param scale: config scalar, default value is one.
411-
:type scale: float
412406
:return: A DotMulProjection Object.
413407
:rtype: DotMulProjection
414408
"""
415409
proj = DotMulProjection(input_layer_name=input.name,
416-
size=input.size,
417-
**param_attr.attr)
418-
proj.origin = input
410+
size=input.size,
411+
**param_attr.attr)
412+
proj.origin = input
419413
return proj
420414

421415
def dotmul_operator(x, y, scale=1):
422416
"""
423417
DotMulOperator takes two inputs and performs element-wise multiplication:
418+
424419
.. math::
425-
out.row[i] += scale * (in1.row[i] .* in2.row[i])
420+
out.row[i] += scale * (x.row[i] .* y.row[i])
421+
426422
where :math:`.*` means element-wise multiplication, and
427423
scale is a config scalar, its default value is one.
424+
428425
The example usage is:
426+
429427
.. code-block:: python
430-
op = dotmul_operator(x, y,
431-
scale=1)
432-
:param input: Input layer
433-
:type input: LayerOutput
428+
429+
op = dotmul_operator(x=layer1, y=layer2, scale=0.5)
430+
431+
:param x: Input layer1
432+
:type x: LayerOutput
433+
:param y: Input layer2
434+
:type y: LayerOutput
434435
:param scale: config scalar, default value is one.
435436
:type scale: float
436437
:return: A DotMulOperator Object.
@@ -487,7 +488,6 @@ def context_projection(input, context_len, context_start=None,
487488
trainable_padding=trainable,
488489
**extra_dict)
489490
proj.origin = input
490-
proj.origin.projection = 'context'
491491
return proj
492492

493493

@@ -2728,7 +2728,6 @@ def conv_operator(img, filter, filter_size, num_filters,
27282728
stride_y=stride_y,
27292729
groups=groups))
27302730
op.origin = [img, filter]
2731-
op.origin.operator = "conv_op"
27322731
return op
27332732

27342733

0 commit comments

Comments
 (0)