tensorflow框架下BERT实体关系联合抽取Python代码,bert4keras
时间: 2024-02-03 15:13:38 浏览: 131
以下是使用bert4keras实现BERT实体关系联合抽取的Python代码示例:
```python
import json
import numpy as np
from bert4keras.backend import keras, K
from bert4keras.layers import Loss
from bert4keras.models import build_transformer_model
from bert4keras.optimizers import Adam
from bert4keras.tokenizers import Tokenizer
from keras.layers import Dense
from keras.models import Model
# 模型参数
maxlen = 128
epochs = 10
batch_size = 16
learning_rate = 2e-5
categories = ["疾病和诊断", "影像检查", "实验室检验", "药物"]
num_classes = len(categories)
# BERT配置
config_path = '/path/to/bert_config.json'
checkpoint_path = '/path/to/bert_model.ckpt'
dict_path = '/path/to/vocab.txt'
# 加载数据
def load_data(filename):
D = []
with open(filename, encoding='utf-8') as f:
for l in f:
l = json.loads(l)
d = {'text': l['text'], 'spo_list': []}
for spo in l['spo_list']:
for o in spo['object']:
d['spo_list'].append((spo['subject'], spo['predicate'], o))
D.append(d)
return D
# 加载数据集
train_data = load_data('/path/to/train_data.json')
valid_data = load_data('/path/to/valid_data.json')
test_data = load_data('/path/to/test_data.json')
# 建立分词器
tokenizer = Tokenizer(dict_path, do_lower_case=True)
class data_generator:
"""数据生成器
"""
def __init__(self, data, batch_size=32, shuffle=True):
self.data = data
self.batch_size = batch_size
self.shuffle = shuffle
self.steps = len(self.data) // self.batch_size
if len(self.data) % self.batch_size != 0:
self.steps += 1
def __len__(self):
return self.steps
def __iter__(self):
while True:
idxs = list(range(len(self.data)))
if self.shuffle:
np.random.shuffle(idxs)
X1, X2, S, Y = [], [], [], []
for i in idxs:
d = self.data[i]
text = d['text'][:maxlen]
x1, x2 = tokenizer.encode(text)
s = np.zeros(len(text))
for spo in d['spo_list']:
subject = spo[0][:maxlen]
object = spo[2][:maxlen]
start = text.find(subject)
if start != -1:
end = start + len(subject) - 1
s[start:end+1] = 1
# 构建标注数据
predicate = spo[1]
y = np.zeros(num_classes)
y[categories.index(predicate)] = 1
X1.append(x1)
X2.append(x2)
S.append(s)
Y.append(y)
if len(X1) == 0:
continue
X1 = keras.preprocessing.sequence.pad_sequences(X1, maxlen=maxlen)
X2 = keras.preprocessing.sequence.pad_sequences(X2, maxlen=maxlen)
S = keras.preprocessing.sequence.pad_sequences(S, maxlen=maxlen)
Y = np.array(Y)
yield [X1, X2, S], Y
# 构建模型
bert_model = build_transformer_model(
config_path,
checkpoint_path,
model='bert',
return_keras_model=False,
)
output_layer = 'Transformer-%s-FeedForward-Norm' % (bert_model.num_hidden_layers - 1)
output = bert_model.get_layer(output_layer).output
output = Dense(num_classes, activation='sigmoid')(output)
model = Model(bert_model.input, output)
model.summary()
# 损失函数
class MultiLoss(Loss):
"""多任务学习的损失函数
"""
def compute_loss(self, inputs, mask=None):
y_true, y_pred = inputs
y_true = K.cast(y_true, y_pred.dtype)
loss = K.binary_crossentropy(y_true, y_pred)
return loss
loss = MultiLoss().compute_loss
# 优化器
optimizer = Adam(learning_rate)
# 编译模型
model.compile(loss=loss, optimizer=optimizer)
# 训练模型
train_generator = data_generator(train_data, batch_size)
valid_generator = data_generator(valid_data, batch_size)
test_generator = data_generator(test_data, batch_size)
model.fit_generator(
train_generator.forfit(),
steps_per_epoch=len(train_generator),
epochs=epochs,
validation_data=valid_generator.forfit(),
validation_steps=len(valid_generator)
)
# 评估模型
model.evaluate_generator(test_generator.forfit(), steps=len(test_generator))
# 保存模型
model.save_weights('/path/to/model.weights')
```
阅读全文