Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
曾经的那一瞬间
Models
提交
812caaf6
M
Models
项目概览
曾经的那一瞬间
/
Models
9 个月 前同步成功
通知
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,体验更适合开发者的 AI 搜索 >>
提交
812caaf6
编写于
8月 20, 2023
作者:
A
A. Unique TensorFlower
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
No public description
PiperOrigin-RevId: 558604894
上级
2394a736
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
26 addition
and
9 deletion
+26
-9
official/nlp/modeling/layers/masked_lm.py
official/nlp/modeling/layers/masked_lm.py
+10
-4
official/nlp/modeling/layers/masked_lm_test.py
official/nlp/modeling/layers/masked_lm_test.py
+16
-5
未找到文件。
official/nlp/modeling/layers/masked_lm.py
浏览文件 @
812caaf6
...
...
@@ -81,10 +81,16 @@ class MaskedLM(tf.keras.layers.Layer):
lm_data
=
self
.
layer_norm
(
lm_data
)
lm_data
=
tf
.
matmul
(
lm_data
,
self
.
embedding_table
,
transpose_b
=
True
)
logits
=
tf
.
nn
.
bias_add
(
lm_data
,
self
.
bias
)
masked_positions_length
=
masked_positions
.
shape
.
as_list
()[
1
]
or
tf
.
shape
(
masked_positions
)[
1
]
logits
=
tf
.
reshape
(
logits
,
[
-
1
,
masked_positions_length
,
self
.
_vocab_size
])
masked_positions_length
=
(
masked_positions
.
shape
.
as_list
()[
1
]
or
tf
.
shape
(
masked_positions
)[
1
]
)
batch_size
=
(
masked_positions
.
shape
.
as_list
()[
0
]
or
tf
.
shape
(
masked_positions
)[
0
]
)
logits
=
tf
.
reshape
(
logits
,
[
batch_size
,
masked_positions_length
,
self
.
_vocab_size
],
)
if
self
.
_output_type
==
'logits'
:
return
logits
return
tf
.
nn
.
log_softmax
(
logits
)
...
...
official/nlp/modeling/layers/masked_lm_test.py
浏览文件 @
812caaf6
...
...
@@ -13,7 +13,7 @@
# limitations under the License.
"""Tests for masked language model network."""
from
absl.testing
import
parameterized
import
numpy
as
np
import
tensorflow
as
tf
...
...
@@ -21,7 +21,7 @@ from official.nlp.modeling.layers import masked_lm
from
official.nlp.modeling.networks
import
bert_encoder
class
MaskedLMTest
(
tf
.
test
.
TestCase
):
class
MaskedLMTest
(
tf
.
test
.
TestCase
,
parameterized
.
TestCase
):
def
create_layer
(
self
,
vocab_size
,
...
...
@@ -110,11 +110,20 @@ class MaskedLMTest(tf.test.TestCase):
self
.
assertEqual
(
expected_output_shape
,
outputs
.
shape
)
self
.
assertAllClose
(
ref_outputs
,
outputs
)
def
test_layer_invocation
(
self
):
@
parameterized
.
named_parameters
(
dict
(
testcase_name
=
'default'
,
num_predictions
=
21
,
),
dict
(
testcase_name
=
'zero_predictions'
,
num_predictions
=
0
,
),
)
def
test_layer_invocation
(
self
,
num_predictions
):
vocab_size
=
100
sequence_length
=
32
hidden_size
=
64
num_predictions
=
21
test_layer
=
self
.
create_layer
(
vocab_size
=
vocab_size
,
hidden_size
=
hidden_size
)
...
...
@@ -131,7 +140,9 @@ class MaskedLMTest(tf.test.TestCase):
(
batch_size
,
sequence_length
,
hidden_size
))
masked_position_data
=
np
.
random
.
randint
(
2
,
size
=
(
batch_size
,
num_predictions
))
_
=
model
.
predict
([
lm_input_data
,
masked_position_data
])
res
=
model
.
predict
([
lm_input_data
,
masked_position_data
])
expected_shape
=
(
batch_size
,
num_predictions
,
vocab_size
)
self
.
assertEqual
(
expected_shape
,
res
.
shape
)
def
test_unknown_output_type_fails
(
self
):
with
self
.
assertRaisesRegex
(
ValueError
,
'Unknown `output` value "bad".*'
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录