Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
曾经的那一瞬间
Models
提交
cfef35b1
M
Models
项目概览
曾经的那一瞬间
/
Models
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
Models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
cfef35b1
编写于
8月 01, 2022
作者:
R
Renjie Liu
提交者:
A. Unique TensorFlower
8月 01, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Internal change
PiperOrigin-RevId: 464685466
上级
4647a7aa
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
31 addition
and
9 deletion
+31
-9
official/nlp/configs/bert.py
official/nlp/configs/bert.py
+2
-0
official/nlp/modeling/layers/mobile_bert_layers.py
official/nlp/modeling/layers/mobile_bert_layers.py
+27
-9
official/projects/edgetpu/nlp/experiments/mobilebert_edgetpu_xxs.yaml
...jects/edgetpu/nlp/experiments/mobilebert_edgetpu_xxs.yaml
+1
-0
official/projects/edgetpu/nlp/modeling/model_builder.py
official/projects/edgetpu/nlp/modeling/model_builder.py
+1
-0
未找到文件。
official/nlp/configs/bert.py
浏览文件 @
cfef35b1
...
...
@@ -41,3 +41,5 @@ class PretrainerConfig(base_config.Config):
cls_heads
:
List
[
ClsHeadConfig
]
=
dataclasses
.
field
(
default_factory
=
list
)
mlm_activation
:
str
=
"gelu"
mlm_initializer_range
:
float
=
0.02
# Currently only used for mobile bert.
mlm_output_weights_use_proj
:
bool
=
False
official/nlp/modeling/layers/mobile_bert_layers.py
浏览文件 @
cfef35b1
...
...
@@ -447,6 +447,7 @@ class MobileBertMaskedLM(tf.keras.layers.Layer):
activation
=
None
,
initializer
=
'glorot_uniform'
,
output
=
'logits'
,
output_weights_use_proj
=
False
,
**
kwargs
):
"""Class initialization.
...
...
@@ -457,6 +458,9 @@ class MobileBertMaskedLM(tf.keras.layers.Layer):
uniform initializer.
output: The output style for this layer. Can be either `logits` or
`predictions`.
output_weights_use_proj: Use projection instead of concating extra output
weights, this may reduce the MLM task accuracy but will reduce the model
params as well.
**kwargs: keyword arguments.
"""
super
().
__init__
(
**
kwargs
)
...
...
@@ -469,6 +473,7 @@ class MobileBertMaskedLM(tf.keras.layers.Layer):
(
'Unknown `output` value "%s". `output` can be either "logits" or '
'"predictions"'
)
%
output
)
self
.
_output_type
=
output
self
.
_output_weights_use_proj
=
output_weights_use_proj
def
build
(
self
,
input_shape
):
self
.
_vocab_size
,
embedding_width
=
self
.
embedding_table
.
shape
...
...
@@ -480,11 +485,18 @@ class MobileBertMaskedLM(tf.keras.layers.Layer):
name
=
'transform/dense'
)
if
hidden_size
>
embedding_width
:
self
.
extra_output_weights
=
self
.
add_weight
(
'extra_output_weights'
,
shape
=
(
self
.
_vocab_size
,
hidden_size
-
embedding_width
),
initializer
=
tf_utils
.
clone_initializer
(
self
.
initializer
),
trainable
=
True
)
if
self
.
_output_weights_use_proj
:
self
.
extra_output_weights
=
self
.
add_weight
(
'output_weights_proj'
,
shape
=
(
embedding_width
,
hidden_size
),
initializer
=
tf_utils
.
clone_initializer
(
self
.
initializer
),
trainable
=
True
)
else
:
self
.
extra_output_weights
=
self
.
add_weight
(
'extra_output_weights'
,
shape
=
(
self
.
_vocab_size
,
hidden_size
-
embedding_width
),
initializer
=
tf_utils
.
clone_initializer
(
self
.
initializer
),
trainable
=
True
)
elif
hidden_size
==
embedding_width
:
self
.
extra_output_weights
=
None
else
:
...
...
@@ -509,10 +521,16 @@ class MobileBertMaskedLM(tf.keras.layers.Layer):
if
self
.
extra_output_weights
is
None
:
lm_data
=
tf
.
matmul
(
lm_data
,
self
.
embedding_table
,
transpose_b
=
True
)
else
:
lm_data
=
tf
.
matmul
(
lm_data
,
tf
.
concat
([
self
.
embedding_table
,
self
.
extra_output_weights
],
axis
=
1
),
transpose_b
=
True
)
if
self
.
_output_weights_use_proj
:
lm_data
=
tf
.
matmul
(
lm_data
,
self
.
extra_output_weights
,
transpose_b
=
True
)
lm_data
=
tf
.
matmul
(
lm_data
,
self
.
embedding_table
,
transpose_b
=
True
)
else
:
lm_data
=
tf
.
matmul
(
lm_data
,
tf
.
concat
([
self
.
embedding_table
,
self
.
extra_output_weights
],
axis
=
1
),
transpose_b
=
True
)
logits
=
tf
.
nn
.
bias_add
(
lm_data
,
self
.
bias
)
masked_positions_length
=
masked_positions
.
shape
.
as_list
()[
1
]
or
tf
.
shape
(
...
...
official/projects/edgetpu/nlp/experiments/mobilebert_edgetpu_xxs.yaml
浏览文件 @
cfef35b1
...
...
@@ -63,6 +63,7 @@ student_model:
type
:
mobilebert
mlm_activation
:
relu
mlm_initializer_range
:
0.02
mlm_output_weights_use_proj
:
true
teacher_model
:
cls_heads
:
[]
encoder
:
...
...
official/projects/edgetpu/nlp/modeling/model_builder.py
浏览文件 @
cfef35b1
...
...
@@ -85,6 +85,7 @@ def build_bert_pretrainer(pretrainer_cfg: params.PretrainerModelParams,
activation
=
tf_utils
.
get_activation
(
pretrainer_cfg
.
mlm_activation
),
initializer
=
tf
.
keras
.
initializers
.
TruncatedNormal
(
stddev
=
pretrainer_cfg
.
mlm_initializer_range
),
output_weights_use_proj
=
pretrainer_cfg
.
mlm_output_weights_use_proj
,
name
=
'cls/predictions'
)
pretrainer
=
edgetpu_pretrainer
.
MobileBERTEdgeTPUPretrainer
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录