simbert文本相似度,短文本语义匹配模型

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

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 = []
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值