simbert文本相似语义召回;保存及在线服务https://blog.csdn.net/weixin_42357472/article/details/116205077
SimBERT(基于UniLM思想、融检索与生成于一体的BERT模型)【主要应用场景:相似文本生成、相似文本检索】
https://blog.csdn.net/u013250861/article/details/123649047
import numpy as np
import os
from collections import Counter
os.environ['TF_KERAS'] = '1'
from bert4keras.backend import keras, K
from bert4keras.models import build_transformer_model
from bert4keras.tokenizers import Tokenizer
from bert4keras.snippets import sequence_padding
from bert4keras.snippets import uniout
from keras.models import Model
maxlen = 32
# bert配置
# bert配置
config_path = r'D:***t\chinese_simbert_L-6_H-384_A-12\bert_config.json'
checkpoint_path = r'D:\*****rt\chinese_simbert_L-6_H-384_A-12\bert_model.ckpt'
dict_path = r'D:\****rt\chinese_simbert_L-6_H-384_A-12\vocab.txt'
# 建立分词器
tokenizer = Tokenizer(dict_path, do_lower_case=True) # 建立分词器
# 建立加载模型
bert = build_transformer_model(
config_path,
checkpoint_path,
with_pool='linear',
application='unilm',
return_keras_model=False,
)
encoder = keras.models.Model(bert.model.inputs, bert.model.outputs[0])
import pandas as pd
datas1 = pd.read_csv(r'D:****raw_datas150.csv')
datas_all = list(datas1["title"])
# 测试相似度效果
data = datas_all
a_token_ids, b_token_ids, labels = [], [], []
texts = []

该博客介绍了如何利用基于 UniLM 的 SimBERT 模型进行相似文本生成和检索。首先,通过 Bert4Keras 库加载预训练的 SimBERT 模型,并构建分词器。接着,对数据集进行预处理,计算每个文本的向量表示并保存。最后,展示了如何实现一个函数,用于输入新文本并返回最相似的 topn 句子。此外,还演示了使用 PaddleNLP TaskFlow 的文本相似度任务,下载并加载 SimBERT 模型进行相似度匹配。

593

被折叠的 条评论
为什么被折叠?



