Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
d7541747
M
models
项目概览
PaddlePaddle
/
models
大约 1 年 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
d7541747
编写于
6月 22, 2019
作者:
P
pkpk
提交者:
Yibing Liu
6月 22, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove reduce_sum/mean to support fp16 (#2492)
上级
1c13f704
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
70 addition
and
56 deletion
+70
-56
PaddleNLP/models/dialogue_model_toolkit/dialogue_general_understanding/define_paradigm.py
...toolkit/dialogue_general_understanding/define_paradigm.py
+70
-56
未找到文件。
PaddleNLP/models/dialogue_model_toolkit/dialogue_general_understanding/define_paradigm.py
浏览文件 @
d7541747
...
...
@@ -20,17 +20,18 @@ import paddle
import
paddle.fluid
as
fluid
class
Paradigm
(
object
):
class
Paradigm
(
object
):
"""
define network paradigm
"""
def
__init__
(
self
,
task_name
):
def
__init__
(
self
,
task_name
):
"""
init
"""
self
.
task_name
=
task_name
def
create_cls
(
self
,
transformer_inst
,
params
):
def
create_cls
(
self
,
transformer_inst
,
params
):
"""
create classify paradigm network
"""
...
...
@@ -48,42 +49,46 @@ class Paradigm(object):
bias_attr
=
fluid
.
ParamAttr
(
name
=
"cls_out_b"
,
initializer
=
fluid
.
initializer
.
Constant
(
0.
)))
if
params
[
'is_prediction'
]:
if
params
[
'is_prediction'
]:
probs
=
fluid
.
layers
.
softmax
(
logits
)
feed_targets_name
=
[
params
[
'src_ids'
].
name
,
params
[
'pos_ids'
].
name
,
params
[
'sent_ids'
].
name
,
params
[
'input_mask'
].
name
,
]
results
=
{
"probs"
:
probs
,
"feed_targets_name"
:
feed_targets_name
}
params
[
'src_ids'
].
name
,
params
[
'pos_ids'
].
name
,
params
[
'sent_ids'
].
name
,
params
[
'input_mask'
].
name
,
]
results
=
{
"probs"
:
probs
,
"feed_targets_name"
:
feed_targets_name
}
return
results
ce_loss
,
probs
=
fluid
.
layers
.
softmax_with_cross_entropy
(
logits
=
logits
,
label
=
params
[
'labels'
],
return_softmax
=
True
)
loss
=
fluid
.
layers
.
reduce_
mean
(
input
=
ce_loss
)
loss
=
fluid
.
layers
.
mean
(
input
=
ce_loss
)
num_seqs
=
fluid
.
layers
.
create_tensor
(
dtype
=
'int64'
)
accuracy
=
fluid
.
layers
.
accuracy
(
input
=
probs
,
label
=
params
[
'labels'
],
total
=
num_seqs
)
accuracy
=
fluid
.
layers
.
accuracy
(
input
=
probs
,
label
=
params
[
'labels'
],
total
=
num_seqs
)
loss
.
persistable
=
True
probs
.
persistable
=
True
accuracy
.
persistable
=
True
num_seqs
.
persistable
=
True
results
=
{
"loss"
:
loss
,
"probs"
:
probs
,
"accuracy"
:
accuracy
,
"num_seqs"
:
num_seqs
}
results
=
{
"loss"
:
loss
,
"probs"
:
probs
,
"accuracy"
:
accuracy
,
"num_seqs"
:
num_seqs
}
return
results
def
create_multi_cls
(
self
,
transformer_inst
,
params
):
def
create_multi_cls
(
self
,
transformer_inst
,
params
):
"""
create multi classify paradigm network
"""
cls_feats
=
transformer_inst
.
get_pooled_output
()
cls_feats
=
fluid
.
layers
.
dropout
(
x
=
cls_feats
,
dropout_prob
=
0.1
,
dropout_implementation
=
"upscale_in_train"
)
x
=
cls_feats
,
dropout_prob
=
0.1
,
dropout_implementation
=
"upscale_in_train"
)
logits
=
fluid
.
layers
.
fc
(
input
=
cls_feats
,
size
=
params
[
'num_labels'
],
...
...
@@ -94,30 +99,32 @@ class Paradigm(object):
name
=
"cls_out_b"
,
initializer
=
fluid
.
initializer
.
Constant
(
0.
)))
labels_onehot
=
fluid
.
layers
.
cast
(
params
[
"labels"
],
dtype
=
'float32'
)
ce_loss
=
fluid
.
layers
.
reduce_sum
(
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
x
=
logits
,
label
=
labels_onehot
))
loss
=
fluid
.
layers
.
reduce_mean
(
input
=
ce_loss
)
ce_loss
=
fluid
.
layers
.
reduce_sum
(
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
x
=
logits
,
label
=
labels_onehot
))
loss
=
fluid
.
layers
.
mean
(
input
=
ce_loss
)
probs
=
fluid
.
layers
.
sigmoid
(
logits
)
if
params
[
'is_prediction'
]:
if
params
[
'is_prediction'
]:
feed_targets_name
=
[
params
[
'src_ids'
].
name
,
params
[
'pos_ids'
].
name
,
params
[
'sent_ids'
].
name
,
params
[
'input_mask'
].
name
,
]
results
=
{
"probs"
:
probs
,
"feed_targets_name"
:
feed_targets_name
}
params
[
'src_ids'
].
name
,
params
[
'pos_ids'
].
name
,
params
[
'sent_ids'
].
name
,
params
[
'input_mask'
].
name
,
]
results
=
{
"probs"
:
probs
,
"feed_targets_name"
:
feed_targets_name
}
return
results
num_seqs
=
fluid
.
layers
.
tensor
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int64'
,
value
=
1
)
num_seqs
=
fluid
.
layers
.
tensor
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int64'
,
value
=
1
)
loss
.
persistable
=
True
probs
.
persistable
=
True
num_seqs
.
persistable
=
True
results
=
{
"loss"
:
loss
,
"probs"
:
probs
,
"num_seqs"
:
num_seqs
}
results
=
{
"loss"
:
loss
,
"probs"
:
probs
,
"num_seqs"
:
num_seqs
}
return
results
def
create_sequence_tagging
(
self
,
transformer_inst
,
params
):
def
create_sequence_tagging
(
self
,
transformer_inst
,
params
):
"""
create sequence tagging paradigm
"""
...
...
@@ -127,52 +134,59 @@ class Paradigm(object):
output_layer
=
fluid
.
layers
.
reshape
(
output_layer
,
[
-
1
,
hidden_size
])
logits
=
fluid
.
layers
.
fc
(
input
=
output_layer
,
size
=
params
[
'num_labels'
])
probs
=
fluid
.
layers
.
cast
(
fluid
.
layers
.
argmax
(
logits
,
axis
=
1
),
dtype
=
'int32'
)
probs
=
fluid
.
layers
.
cast
(
fluid
.
layers
.
argmax
(
logits
,
axis
=
1
),
dtype
=
'int32'
)
if
params
[
'is_prediction'
]:
if
params
[
'is_prediction'
]:
feed_targets_name
=
[
params
[
'src_ids'
].
name
,
params
[
'pos_ids'
].
name
,
params
[
'sent_ids'
].
name
,
params
[
'input_mask'
].
name
,
]
results
=
{
"probs"
:
probs
,
"feed_targets_name"
:
feed_targets_name
}
params
[
'src_ids'
].
name
,
params
[
'pos_ids'
].
name
,
params
[
'sent_ids'
].
name
,
params
[
'input_mask'
].
name
,
]
results
=
{
"probs"
:
probs
,
"feed_targets_name"
:
feed_targets_name
}
return
results
num_seqs
=
fluid
.
layers
.
tensor
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int64'
,
value
=
1
)
y_label_reshape
=
fluid
.
layers
.
cast
(
fluid
.
layers
.
reshape
(
params
[
'labels'
],
[
-
1
]),
dtype
=
'int32'
)
num_seqs
=
fluid
.
layers
.
tensor
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int64'
,
value
=
1
)
y_label_reshape
=
fluid
.
layers
.
cast
(
fluid
.
layers
.
reshape
(
params
[
'labels'
],
[
-
1
]),
dtype
=
'int32'
)
correct_prediction
=
fluid
.
layers
.
equal
(
probs
,
y_label_reshape
)
accuracy
=
fluid
.
layers
.
reduce_mean
(
fluid
.
layers
.
cast
(
correct_prediction
,
dtype
=
'float32'
))
accuracy
=
fluid
.
layers
.
mean
(
fluid
.
layers
.
cast
(
correct_prediction
,
dtype
=
'float32'
))
ce_loss
=
fluid
.
layers
.
softmax_with_cross_entropy
(
logits
=
logits
,
\
label
=
fluid
.
layers
.
reshape
(
params
[
'labels'
],
[
-
1
,
1
]))
loss
=
fluid
.
layers
.
reduce_
mean
(
input
=
ce_loss
)
loss
=
fluid
.
layers
.
mean
(
input
=
ce_loss
)
loss
.
persistable
=
True
probs
.
persistable
=
True
accuracy
.
persistable
=
True
num_seqs
.
persistable
=
True
results
=
{
"loss"
:
loss
,
"probs"
:
probs
,
"accuracy"
:
accuracy
,
"num_seqs"
:
num_seqs
}
results
=
{
"loss"
:
loss
,
"probs"
:
probs
,
"accuracy"
:
accuracy
,
"num_seqs"
:
num_seqs
}
return
results
def
paradigm
(
self
,
transformer_inst
,
params
):
def
paradigm
(
self
,
transformer_inst
,
params
):
"""
run paradigm
"""
results
=
None
if
self
.
task_name
==
'udc'
:
if
self
.
task_name
==
'udc'
:
results
=
self
.
create_cls
(
transformer_inst
,
params
)
elif
self
.
task_name
==
'swda'
:
results
=
self
.
create_cls
(
transformer_inst
,
params
)
elif
self
.
task_name
==
'mrda'
:
elif
self
.
task_name
==
'mrda'
:
results
=
self
.
create_cls
(
transformer_inst
,
params
)
elif
self
.
task_name
==
'atis_intent'
:
elif
self
.
task_name
==
'atis_intent'
:
results
=
self
.
create_cls
(
transformer_inst
,
params
)
elif
self
.
task_name
==
'atis_slot'
:
elif
self
.
task_name
==
'atis_slot'
:
results
=
self
.
create_sequence_tagging
(
transformer_inst
,
params
)
elif
self
.
task_name
==
'dstc2'
:
results
=
self
.
create_multi_cls
(
transformer_inst
,
params
)
return
results
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录