Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
Ailearning
提交
2feccb35
A
Ailearning
项目概览
OpenDocCN
/
Ailearning
8 个月 前同步成功
通知
7
Star
36240
Fork
11272
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
Ailearning
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
2feccb35
编写于
12月 30, 2022
作者:
片刻小哥哥
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
还原删除代码
上级
8b94b3e8
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
58 addition
and
0 deletion
+58
-0
src/py3.x/tensorflow2.x/text_bert.py
src/py3.x/tensorflow2.x/text_bert.py
+58
-0
未找到文件。
src/py3.x/tensorflow2.x/text_bert.py
浏览文件 @
2feccb35
...
...
@@ -93,6 +93,64 @@ if __name__ == "__main__":
## bert / Embedding/ + lstm + crt
#%%
# 加载数据
class
TextBert
():
def
__init__
(
self
):
self
.
path_config
=
Config
.
bert
.
path_config
self
.
path_checkpoint
=
Config
.
bert
.
path_checkpoint
self
.
token_dict
=
{}
with
codecs
.
open
(
Config
.
bert
.
dict_path
,
'r'
,
'utf8'
)
as
reader
:
for
line
in
reader
:
token
=
line
.
strip
()
self
.
token_dict
[
token
]
=
len
(
self
.
token_dict
)
def
prepare_data
(
self
):
neg
=
pd
.
read_excel
(
Config
.
bert
.
path_neg
,
header
=
None
)
pos
=
pd
.
read_excel
(
Config
.
bert
.
path_pos
,
header
=
None
)
data
=
[]
for
d
in
neg
[
0
]:
data
.
append
((
d
,
0
))
for
d
in
pos
[
0
]:
data
.
append
((
d
,
1
))
# 按照9:1的比例划分训练集和验证集
random_order
=
list
(
range
(
len
(
data
)))
np
.
random
.
shuffle
(
random_order
)
train_data
=
[
data
[
j
]
for
i
,
j
in
enumerate
(
random_order
)
if
i
%
10
!=
0
]
valid_data
=
[
data
[
j
]
for
i
,
j
in
enumerate
(
random_order
)
if
i
%
10
==
0
]
return
train_data
,
valid_data
def
build_model
(
self
,
m_type
=
"bert"
):
if
m_type
==
"bert"
:
bert_model
=
load_trained_model_from_checkpoint
(
self
.
path_config
,
self
.
path_checkpoint
,
seq_len
=
None
)
for
l
in
bert_model
.
layers
:
l
.
trainable
=
True
x1_in
=
Input
(
shape
=
(
None
,))
x2_in
=
Input
(
shape
=
(
None
,))
x
=
bert_model
([
x1_in
,
x2_in
])
x
=
Lambda
(
lambda
x
:
x
[:,
0
])(
x
)
p
=
Dense
(
1
,
activation
=
'sigmoid'
)(
x
)
#根据分类种类自行调节,也可以多加一些层数
model
=
Model
([
x1_in
,
x2_in
],
p
)
model
.
compile
(
loss
=
'binary_crossentropy'
,
optimizer
=
Adam
(
1e-5
),
# 用足够小的学习率
metrics
=
[
'accuracy'
]
)
else
:
# 否则用 Embedding
model
=
Sequential
()
model
.
add
(
Embedding
(
len
(
vocab
),
EMBED_DIM
,
mask_zero
=
True
))
# Random embedding
model
.
add
(
Bidirectional
(
LSTM
(
BiRNN_UNITS
//
2
,
return_sequences
=
True
)))
crf
=
CRF
(
len
(
chunk_tags
),
sparse_target
=
True
)
model
.
add
(
crf
)
model
.
compile
(
'adam'
,
loss
=
crf
.
loss_function
,
metrics
=
[
crf
.
accuracy
])
model
.
summary
()
return
model
#%%
# 加载数据
from
keras_bert
import
Tokenizer
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录