提交 83ce2dce 编写于 作者: C caoying03

split sorting into another layer. fix config helper.

上级 00b6d266
...@@ -497,8 +497,6 @@ message LayerConfig { ...@@ -497,8 +497,6 @@ message LayerConfig {
repeated uint32 offset = 55; repeated uint32 offset = 55;
repeated uint32 shape = 56; repeated uint32 shape = 56;
// for sub_nest_seq layer to select top k sequence with highest scores
optional uint32 top_k = 57 [default = 1];
} }
message EvaluatorConfig { message EvaluatorConfig {
......
...@@ -2659,22 +2659,28 @@ class SubSequenceLayer(LayerBase): ...@@ -2659,22 +2659,28 @@ class SubSequenceLayer(LayerBase):
@config_layer('sub_nested_seq') @config_layer('sub_nested_seq')
class SubNestedSequenceLayer(LayerBase): class SubNestedSequenceLayer(LayerBase):
def __init__(self, name, inputs, top_k=1, bias=False, **xargs): def __init__(self, name, inputs, selected_indices, bias=False, **xargs):
if isinstance(inputs, list):
assert len(inputs) == 1, ('the first input of sub_nested_seq '
'layer is a single nested sequence.')
inputs = inputs[0]
if isinstance(selected_indices, list):
assert len(selected_indices) == 1, (
'the second input of '
'sub_nested_seq layer is a single layer which is a '
'set of selected indices.')
selected_indices = selected_indices[0]
super(SubNestedSequenceLayer, self).__init__( super(SubNestedSequenceLayer, self).__init__(
name, 'sub_nested_seq', 0, inputs=inputs, **xargs) name,
config_assert( 'sub_nested_seq',
len(inputs) == 2, 0,
('SubNestSequenceLayer must have 2 inputs: ' inputs=[inputs, selected_indices],
'input1 is a nested sequence; input2 is a learnable distribution ' **xargs)
'or scores over each sentence in the nested sequence. '))
input_layer0 = self.get_input_layer(0) input_layer0 = self.get_input_layer(0)
size = input_layer0.size size = input_layer0.size
self.set_layer_size(size) self.set_layer_size(size)
self.config.top_k = top_k
input_layer1 = self.get_input_layer(1)
assert (input_layer1.size == 1)
@config_layer('out_prod') @config_layer('out_prod')
class OuterProdLayer(LayerBase): class OuterProdLayer(LayerBase):
......
...@@ -6092,37 +6092,41 @@ def crop_layer(input, offset, axis=2, shape=None, name=None, layer_attr=None): ...@@ -6092,37 +6092,41 @@ def crop_layer(input, offset, axis=2, shape=None, name=None, layer_attr=None):
@wrap_name_default() @wrap_name_default()
@layer_support() @layer_support()
def sub_nested_seq_layer(input, name=None, top_k=1): def sub_nested_seq_layer(input, selected_indices, name=None):
""" """
The sub_nested_seq_layer accepts two inputs: the first one is a nested The sub_nested_seq_layer accepts two inputs: the first one is a nested
sequence in PaddlePaddle; the second one is a learnable score or sequence; the second one is a set of selceted indices in the nested sequence.
distribution over each sequence in the nested sequence.
Then sub_nest_seq_layer selects top k sentences with highest scores or
probabilites according to the second input. Then sub_nest_seq_layer selects trims the first input according to the
selected indices to give a new output. This layer is used in beam training.
The example usage is: The example usage is:
.. code-block:: python .. code-block:: python
prob = fc_layer(input=data, size=1, act=SequenceSoftmaxActivation()) sub_nest_seq = sub_nested_seq_layer(input=[data, selected_indices])
sub_nest_seq = sub_nested_seq_layer(input=[data, prob], top_k=3)
:param input: The two input layers. The first input must be a nested :param input: A nested sequence.
sequence. The second input is a learnable scores, whose size must be 1. :type input: LayerOutput
:param selected_indices: a set of sequence indices in the nested sequence.
:type input: LayerOutput :type input: LayerOutput
:param name: name of this layer. :param name: name of this layer.
:type name: basestring :type name: basestring
:param top_k: number of sequences with highest probabilies to select.
:type top_k: int
:return: LayerOutput object. :return: LayerOutput object.
:rtype: LayerOutput :rtype: LayerOutput
""" """
assert isinstance(input, collections.Sequence) and len(input) == 2, ( assert isinstance(input, LayerOutput), (
'sub_nest_seq_layer has exactly two inputs.') 'The first input of '
'sub_nested_seq_layer must be a Paddle layer.')
assert isinstance(selected_indices, LayerOutput), (
'The second input of '
'sub_nested_seq_layer must be a Paddle layer.')
l = Layer( l = Layer(
inputs=[x.name for x in input], inputs=input.name,
selected_indices=selected_indices.name,
name=name, name=name,
top_k=top_k,
type=LayerType.SUB_NESTED_SEQ) type=LayerType.SUB_NESTED_SEQ)
return LayerOutput( return LayerOutput(
name=name, name=name,
......
type: "nn" type: "nn"
layers { layers {
name: "input" name: "input_seq"
type: "data" type: "data"
size: 300 size: 300
active_type: "" active_type: ""
} }
layers { layers {
name: "__fc_layer_0__" name: "input"
type: "fc" type: "data"
size: 1 size: 5
active_type: "sequence_softmax" active_type: ""
inputs {
input_layer_name: "input"
input_parameter_name: "___fc_layer_0__.w0"
}
bias_parameter_name: "___fc_layer_0__.wbias"
} }
layers { layers {
name: "__sub_nested_seq_layer_0__" name: "__sub_nested_seq_layer_0__"
...@@ -22,41 +17,20 @@ layers { ...@@ -22,41 +17,20 @@ layers {
size: 300 size: 300
active_type: "" active_type: ""
inputs { inputs {
input_layer_name: "input" input_layer_name: "input_seq"
} }
inputs { inputs {
input_layer_name: "__fc_layer_0__" input_layer_name: "input"
} }
top_k: 1
}
parameters {
name: "___fc_layer_0__.w0"
size: 300
initial_mean: 0.0
initial_std: 0.057735026919
dims: 300
dims: 1
initial_strategy: 0
initial_smart: true
}
parameters {
name: "___fc_layer_0__.wbias"
size: 1
initial_mean: 0.0
initial_std: 0.0
dims: 1
dims: 1
initial_strategy: 0
initial_smart: false
} }
input_layer_names: "input" input_layer_names: "input_seq"
output_layer_names: "__sub_nested_seq_layer_0__" output_layer_names: "__sub_nested_seq_layer_0__"
sub_models { sub_models {
name: "root" name: "root"
layer_names: "input_seq"
layer_names: "input" layer_names: "input"
layer_names: "__fc_layer_0__"
layer_names: "__sub_nested_seq_layer_0__" layer_names: "__sub_nested_seq_layer_0__"
input_layer_names: "input" input_layer_names: "input_seq"
output_layer_names: "__sub_nested_seq_layer_0__" output_layer_names: "__sub_nested_seq_layer_0__"
is_recurrent_layer_group: false is_recurrent_layer_group: false
} }
......
...@@ -2,8 +2,10 @@ ...@@ -2,8 +2,10 @@
#coding=utf-8 #coding=utf-8
from paddle.trainer_config_helpers import * from paddle.trainer_config_helpers import *
data = data_layer(name='input', size=300) beam_size = 5
prob = fc_layer(input=data, size=1, act=SequenceSoftmaxActivation())
sub_nest_seq = sub_nested_seq_layer(input=[data, prob], top_k=1) data = data_layer(name='input_seq', size=300)
selected_ids = data_layer(name='input', size=beam_size)
sub_nest_seq = sub_nested_seq_layer(input=data, selected_indices=selected_ids)
outputs(sub_nest_seq) outputs(sub_nest_seq)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册