Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
44ae44da
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
44ae44da
编写于
8月 14, 2017
作者:
C
caoying03
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add configuratioin helpers.
上级
452f3cc0
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
295 addition
and
4 deletion
+295
-4
python/paddle/trainer/config_parser.py
python/paddle/trainer/config_parser.py
+16
-0
python/paddle/trainer_config_helpers/layers.py
python/paddle/trainer_config_helpers/layers.py
+31
-3
python/paddle/trainer_config_helpers/tests/configs/file_list.sh
.../paddle/trainer_config_helpers/tests/configs/file_list.sh
+1
-1
python/paddle/trainer_config_helpers/tests/configs/protostr/test_cross_entropy_over_beam.protostr
...ts/configs/protostr/test_cross_entropy_over_beam.protostr
+208
-0
python/paddle/trainer_config_helpers/tests/configs/test_cross_entropy_over_beam.py
...fig_helpers/tests/configs/test_cross_entropy_over_beam.py
+39
-0
未找到文件。
python/paddle/trainer/config_parser.py
浏览文件 @
44ae44da
...
...
@@ -1602,6 +1602,21 @@ class MultiClassCrossEntropySelfNormCostLayer(LayerBase):
self
.
config
.
softmax_selfnorm_alpha
=
softmax_selfnorm_alpha
@
config_layer
(
'cross_entropy_over_beam'
)
class
CrossEntropyOverBeamLayer
(
LayerBase
):
def
__init__
(
self
,
name
,
inputs
,
**
xargs
):
config_assert
(
len
(
inputs
)
%
3
==
0
,
"Error input numbers."
)
super
(
CrossEntropyOverBeamLayer
,
self
).
__init__
(
name
,
'cross_entropy_over_beam'
,
0
,
inputs
,
**
xargs
)
input_num
=
len
(
inputs
)
/
3
for
i
in
range
(
input_num
):
input_layer
=
self
.
get_input_layer
(
i
*
2
)
config_assert
(
input_layer
.
size
==
1
,
"Inputs for this layer are made up of "
"several pairs and the first one in a pair is scores for "
"all the candidates, so its size should be equal to 1."
)
@
config_layer
(
'fc'
)
class
FCLayer
(
LayerBase
):
layer_type
=
'fc'
...
...
@@ -2249,6 +2264,7 @@ def define_cost(class_name, cost_type):
define_cost
(
'MultiClassCrossEntropy'
,
'multi-class-cross-entropy'
)
define_cost
(
'CrossEntropyOverBeamCostLayer'
,
'cross_entropy_over_beam'
)
define_cost
(
'RankingCost'
,
'rank-cost'
)
define_cost
(
'AucValidation'
,
'auc-validation'
)
define_cost
(
'PnpairValidation'
,
'pnpair-validation'
)
...
...
python/paddle/trainer_config_helpers/layers.py
浏览文件 @
44ae44da
...
...
@@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
functools
import
collections
import
inspect
...
...
@@ -104,6 +103,7 @@ __all__ = [
'nce_layer'
,
'cross_entropy_with_selfnorm'
,
'cross_entropy'
,
'cross_entropy_over_beam'
,
'multi_binary_label_cross_entropy'
,
'sum_cost'
,
'rank_cost'
,
...
...
@@ -219,6 +219,7 @@ class LayerType(object):
HUBER
=
'huber'
CROSS_ENTROPY
=
'multi-class-cross-entropy'
CROSS_ENTROPY_WITH_SELFNORM
=
'multi_class_cross_entropy_with_selfnorm'
CROSS_ENTROPY_OVER_BEAM
=
'cross_entropy_over_beam'
SOFT_BIN_CLASS_CROSS_ENTROPY
=
'soft_binary_class_cross_entropy'
MULTI_BIN_LABEL_CROSS_ENTROPY
=
'multi_binary_label_cross_entropy'
SUM_COST
=
'sum_cost'
...
...
@@ -4028,8 +4029,12 @@ def __cost_input__(input, label, weight=None):
"""
inputs and parents for cost layers.
"""
ipts
=
[
Input
(
input
.
name
),
Input
(
label
.
name
)]
parents
=
[
input
,
label
]
if
isinstance
(
input
,
LayerOutput
):
input
=
[
input
]
if
isinstance
(
label
,
LayerOutput
):
label
=
[
label
]
ipts
=
[
Input
(
ipt
.
name
)
for
ipt
in
(
input
+
label
)]
parents
=
[
ipt
for
ipt
in
(
input
+
label
)]
if
weight
is
not
None
:
assert
weight
.
size
==
1
ipts
.
append
(
Input
(
weight
.
name
))
...
...
@@ -5692,6 +5697,29 @@ def multi_binary_label_cross_entropy(input,
size
=
1
)
@
wrap_name_default
()
@
layer_support
()
def
cross_entropy_over_beam
(
input
,
label
,
name
=
None
,
coeff
=
1.0
,
weight
=
None
):
"""
TODO(caoying) add comments.
"""
assert
len
(
input
)
/
2
==
len
(
label
),
"Error input numbers."
for
i
in
range
(
0
,
len
(
input
),
2
):
assert
(
input
[
i
].
size
==
1
),
(
"Inputs for this layer are made up of "
"several pairs and the first one in a pair is scores for "
"all the candidates, so its size should be equal to 1."
)
ipts
,
parents
=
__cost_input__
(
input
,
label
,
weight
)
Layer
(
name
=
name
,
type
=
LayerType
.
CROSS_ENTROPY_OVER_BEAM
,
inputs
=
ipts
,
coeff
=
coeff
)
return
LayerOutput
(
name
,
LayerType
.
CROSS_ENTROPY
,
parents
=
parents
,
size
=
1
)
@
wrap_name_default
()
@
layer_support
()
def
smooth_l1_cost
(
input
,
label
,
name
=
None
,
coeff
=
1.0
,
layer_attr
=
None
):
...
...
python/paddle/trainer_config_helpers/tests/configs/file_list.sh
浏览文件 @
44ae44da
...
...
@@ -8,6 +8,6 @@ test_spp_layer test_bilinear_interp test_maxout test_bi_grumemory math_ops
test_seq_concat_reshape test_pad test_smooth_l1 test_multiplex_layer
test_prelu_layer test_row_conv test_detection_output_layer test_multibox_loss_layer
test_recursive_topology test_gated_unit_layer test_clip_layer test_row_l2_norm_layer
test_kmax_seq_socre_layer test_seq_select_layers
)
test_kmax_seq_socre_layer test_seq_select_layers
test_cross_entropy_over_beam
)
export
whole_configs
=(
test_split_datasource
)
python/paddle/trainer_config_helpers/tests/configs/protostr/test_cross_entropy_over_beam.protostr
0 → 100644
浏览文件 @
44ae44da
type: "nn"
layers {
name: "sentence_states"
type: "data"
size: 32
active_type: ""
}
layers {
name: "sentence_scores"
type: "data"
size: 1
active_type: ""
}
layers {
name: "__kmax_sequence_score_layer_0__"
type: "kmax_seq_score"
active_type: ""
inputs {
input_layer_name: "sentence_scores"
}
beam_size: 5
}
layers {
name: "__sub_nested_seq_layer_0__"
type: "sub_nested_seq"
size: 32
active_type: ""
inputs {
input_layer_name: "sentence_states"
}
inputs {
input_layer_name: "__kmax_sequence_score_layer_0__"
}
}
layers {
name: "__fc_layer_0__"
type: "fc"
size: 1
active_type: ""
inputs {
input_layer_name: "__sub_nested_seq_layer_0__"
input_parameter_name: "___fc_layer_0__.w0"
}
bias_parameter_name: "___fc_layer_0__.wbias"
}
layers {
name: "__kmax_sequence_score_layer_1__"
type: "kmax_seq_score"
active_type: ""
inputs {
input_layer_name: "sentence_scores"
}
beam_size: 5
}
layers {
name: "__seq_slice_layer_0__"
type: "seq_slice"
size: 32
active_type: ""
inputs {
input_layer_name: "__sub_nested_seq_layer_0__"
}
inputs {
input_layer_name: "__kmax_sequence_score_layer_1__"
}
select_first: true
}
layers {
name: "__fc_layer_1__"
type: "fc"
size: 1
active_type: ""
inputs {
input_layer_name: "__seq_slice_layer_0__"
input_parameter_name: "___fc_layer_1__.w0"
}
bias_parameter_name: "___fc_layer_1__.wbias"
}
layers {
name: "__kmax_sequence_score_layer_2__"
type: "kmax_seq_score"
active_type: ""
inputs {
input_layer_name: "__fc_layer_1__"
}
beam_size: 5
}
layers {
name: "sentences_ids"
type: "data"
size: 1
active_type: ""
}
layers {
name: "start_ids"
type: "data"
size: 1
active_type: ""
}
layers {
name: "end_ids"
type: "data"
size: 1
active_type: ""
}
layers {
name: "__cross_entropy_over_beam_0__"
type: "cross_entropy_over_beam"
active_type: ""
inputs {
input_layer_name: "sentence_scores"
}
inputs {
input_layer_name: "__kmax_sequence_score_layer_0__"
}
inputs {
input_layer_name: "__fc_layer_0__"
}
inputs {
input_layer_name: "__kmax_sequence_score_layer_1__"
}
inputs {
input_layer_name: "__fc_layer_1__"
}
inputs {
input_layer_name: "__kmax_sequence_score_layer_2__"
}
inputs {
input_layer_name: "sentences_ids"
}
inputs {
input_layer_name: "start_ids"
}
inputs {
input_layer_name: "end_ids"
}
coeff: 1.0
}
parameters {
name: "___fc_layer_0__.w0"
size: 32
initial_mean: 0.0
initial_std: 0.176776695297
dims: 32
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
}
parameters {
name: "___fc_layer_1__.w0"
size: 32
initial_mean: 0.0
initial_std: 0.176776695297
dims: 32
dims: 1
initial_strategy: 0
initial_smart: true
}
parameters {
name: "___fc_layer_1__.wbias"
size: 1
initial_mean: 0.0
initial_std: 0.0
dims: 1
dims: 1
initial_strategy: 0
initial_smart: false
}
input_layer_names: "sentence_scores"
input_layer_names: "sentence_states"
input_layer_names: "sentences_ids"
input_layer_names: "start_ids"
input_layer_names: "end_ids"
output_layer_names: "__cross_entropy_over_beam_0__"
sub_models {
name: "root"
layer_names: "sentence_states"
layer_names: "sentence_scores"
layer_names: "__kmax_sequence_score_layer_0__"
layer_names: "__sub_nested_seq_layer_0__"
layer_names: "__fc_layer_0__"
layer_names: "__kmax_sequence_score_layer_1__"
layer_names: "__seq_slice_layer_0__"
layer_names: "__fc_layer_1__"
layer_names: "__kmax_sequence_score_layer_2__"
layer_names: "sentences_ids"
layer_names: "start_ids"
layer_names: "end_ids"
layer_names: "__cross_entropy_over_beam_0__"
input_layer_names: "sentence_scores"
input_layer_names: "sentence_states"
input_layer_names: "sentences_ids"
input_layer_names: "start_ids"
input_layer_names: "end_ids"
output_layer_names: "__cross_entropy_over_beam_0__"
is_recurrent_layer_group: false
}
python/paddle/trainer_config_helpers/tests/configs/test_cross_entropy_over_beam.py
0 → 100644
浏览文件 @
44ae44da
#!/usr/bin/env python
#coding=utf-8
from
paddle.trainer_config_helpers
import
*
beam_size
=
5
# the first beam expansion.
sentence_states
=
data_layer
(
name
=
"sentence_states"
,
size
=
32
)
sentence_scores
=
data_layer
(
name
=
"sentence_scores"
,
size
=
1
)
topk_sentence_ids
=
kmax_sequence_score_layer
(
input
=
sentence_scores
,
beam_size
=
beam_size
)
# the second beam expansion.
topk_sen
=
sub_nested_seq_layer
(
input
=
sentence_states
,
selected_indices
=
topk_sentence_ids
)
start_pos_scores
=
fc_layer
(
input
=
topk_sen
,
size
=
1
,
act
=
LinearActivation
())
topk_start_pos_ids
=
kmax_sequence_score_layer
(
input
=
sentence_scores
,
beam_size
=
beam_size
)
# the final beam expansion.
topk_start_spans
=
seq_slice_layer
(
input
=
topk_sen
,
starts
=
topk_start_pos_ids
,
ends
=
None
)
end_pos_scores
=
fc_layer
(
input
=
topk_start_spans
,
size
=
1
,
act
=
LinearActivation
())
topk_end_pos_ids
=
kmax_sequence_score_layer
(
input
=
end_pos_scores
,
beam_size
=
beam_size
)
# define the cost
sentence_idx
=
data_layer
(
name
=
"sentences_ids"
,
size
=
1
)
start_idx
=
data_layer
(
name
=
"start_ids"
,
size
=
1
)
end_idx
=
data_layer
(
name
=
"end_ids"
,
size
=
1
)
cost
=
cross_entropy_over_beam
(
input
=
[
sentence_scores
,
topk_sentence_ids
,
start_pos_scores
,
topk_start_pos_ids
,
end_pos_scores
,
topk_end_pos_ids
],
label
=
[
sentence_idx
,
start_idx
,
end_idx
])
outputs
(
cost
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录