使用bert_base_chinese实现文本语义相似度计算

本文选择将模型下载到本地,进行离线分析计算,也可以使用在线下载,但本文略过

1 下载bert_base_chinese

下载地址:https://huggingface.co/google-bert/bert-base-chinese/tree/main

下载图上红框内的四个文件,并按照下图的目录结构放置

bert-base-chinese文件夹里放

2 代码

python 复制代码
import torch
from transformers import BertTokenizer, BertModel
from torch.nn.functional import cosine_similarity

# 初始化分词器和模型
vocab_file = 'D:/code/python/rpa/vocab.txt'
tokenizer = BertTokenizer.from_pretrained(vocab_file)
model = BertModel.from_pretrained('D:/code/python/rpa/bert-base-chinese')

def get_bert_embeddings(text):
    # 对文本进行分词
    inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
    # 获取BERT的输出
    with torch.no_grad():
        outputs = model(**inputs)
    # 获取最后一层的隐藏状态
    last_hidden_states = outputs.last_hidden_state
    # 取[CLS]标记的输出作为句子的表示
    sentence_embedding = last_hidden_states[:, 0, :]
    return sentence_embedding

# 计算两个文本的语义相似度
def calculate_similarity(text1, text2):
    emb1 = get_bert_embeddings(text1)
    emb2 = get_bert_embeddings(text2)
    
    # 计算余弦相似度
    # 将emb1和emb2调整为(batch_size, 1, embedding_dim),以便使用cosine_similarity
    similarity = cosine_similarity(emb1.unsqueeze(1), emb2.unsqueeze(1), dim=2)
    return similarity.item()

# 主函数
def main(text1, text2):
    similarity = calculate_similarity(text1, text2)
    print(f"The semantic similarity between the texts is: {similarity}")

text1 = '我的身体很健康'
text2 = '我没有生病'
main(text1, text2)
bash 复制代码
# result
The semantic similarity between the texts is: 0.8934338092803955
相关推荐
子燕若水1 小时前
Unreal Engine 5中的AI知识
人工智能
极限实验室3 小时前
Coco AI 实战(一):Coco Server Linux 平台部署
人工智能
杨过过儿3 小时前
【学习笔记】4.1 什么是 LLM
人工智能
巴伦是只猫3 小时前
【机器学习笔记Ⅰ】13 正则化代价函数
人工智能·笔记·机器学习
伍哥的传说3 小时前
React 各颜色转换方法、颜色值换算工具HEX、RGB/RGBA、HSL/HSLA、HSV、CMYK
深度学习·神经网络·react.js
大千AI助手3 小时前
DTW模版匹配:弹性对齐的时间序列相似度度量算法
人工智能·算法·机器学习·数据挖掘·模版匹配·dtw模版匹配
AI生存日记3 小时前
百度文心大模型 4.5 系列全面开源 英特尔同步支持端侧部署
人工智能·百度·开源·open ai大模型
LCG元4 小时前
自动驾驶感知模块的多模态数据融合:时序同步与空间对齐的框架解析
人工智能·机器学习·自动驾驶
why技术4 小时前
Stack Overflow,轰然倒下!
前端·人工智能·后端