当前位置: 首页 > ds >正文

中文通用embedding:BGE

huggingface:https://huggingface.co/BAAI/bge-small-zh-v1.5
论文地址:https://arxiv.org/abs/2309.07597
模型结构就是BERT,绝对位置编码,使用时取最后一层CLS token作为sentence_embeddings:
MLM训练使用RetroMAE https://arxiv.org/abs/2205.12035 提出的方式

BertModel((embeddings): BertEmbeddings((word_embeddings): Embedding(21128, 512, padding_idx=0)(position_embeddings): Embedding(512, 512)(token_type_embeddings): Embedding(2, 512)(LayerNorm): LayerNorm((512,), eps=1e-12, elementwise_affine=True)(dropout): Dropout(p=0.1, inplace=False))(encoder): BertEncoder((layer): ModuleList((0-3): 4 x BertLayer((attention): BertAttention((self): BertSdpaSelfAttention((query): Linear(in_features=512, out_features=512, bias=True)(key): Linear(in_features=512, out_features=512, bias=True)(value): Linear(in_features=512, out_features=512, bias=True)(dropout): Dropout(p=0.1, inplace=False))(output): BertSelfOutput((dense): Linear(in_features=512, out_features=512, bias=True)(LayerNorm): LayerNorm((512,), eps=1e-12, elementwise_affine=True)(dropout): Dropout(p=0.1, inplace=False)))(intermediate): BertIntermediate((dense): Linear(in_features=512, out_features=2048, bias=True)(intermediate_act_fn): GELUActivation())(output): BertOutput((dense): Linear(in_features=2048, out_features=512, bias=True)(LayerNorm): LayerNorm((512,), eps=1e-12, elementwise_affine=True)(dropout): Dropout(p=0.1, inplace=False)))))(pooler): BertPooler((dense): Linear(in_features=512, out_features=512, bias=True)(activation): Tanh())
)
from transformers import AutoTokenizer, AutoModel
import torch
# Sentences we want sentence embeddings for
sentences = ["样例数据-1", "样例数据-2"]# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-large-zh-v1.5')
model = AutoModel.from_pretrained('BAAI/bge-large-zh-v1.5')
model.eval()# Tokenize sentences
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
# for s2p(short query to long passage) retrieval task, add an instruction to query (not add instruction for passages)
# encoded_input = tokenizer([instruction + q for q in queries], padding=True, truncation=True, return_tensors='pt')# Compute token embeddings
with torch.no_grad():model_output = model(**encoded_input)# Perform pooling. In this case, cls pooling.sentence_embeddings = model_output[0][:, 0]
# normalize embeddings
sentence_embeddings = torch.nn.functional.normalize(sentence_embeddings, p=2, dim=1)
print("Sentence embeddings:", sentence_embeddings)
http://www.xdnf.cn/news/1136.html

相关文章:

  • 使用Spark-TTS-0.5B模型,文本合成语音
  • HCIP(综合实验2)
  • mockMvc构建web单元测试学习笔记
  • Unity-GC详解
  • 面试网络基础知识 — IP地址
  • PyTorch 分布式 DistributedDataParallel (DDP)
  • Nordic外设GPIO[nrfx_gpiote_in_init函数报NRFX_ERROR_NO_MEM并且fatal error]
  • 门控循环单元(GRU)
  • LX10-MDK的使用技巧
  • 【Spring Boot基础】MyBatis的基础操作:增删查改、列名和属性名匹配 -- XML实现
  • 【Java面试笔记:基础】7.int和Integer有什么区别?
  • kubernetes》》k8s》》删除命名空间
  • Spring中配置 Bean 的两种方式:XML 配置 和 Java 配置类
  • 18.2基于Linux的INPUT子系统实验(详细编写程序)_csdn
  • IDEA 创建Maven 工程(图文)
  • dmncdm达梦新云缓存数据库主从集群安装部署详细步骤说明
  • Redis 系列之 Key 过期策略介绍
  • 09.传输层协议 ——— TCP协议
  • kotlin的kmp编程中遇到Unresolved reference ‘java‘问题
  • 鸿蒙应用开发:如何修改APP名称与APP的图标
  • 物联网蓬勃发展是助力楼宇自控技术迈向成熟的关键
  • 中电金信联合阿里云推出智能陪练Agent
  • 智能指针之设计模式4
  • BPC电波授时技术
  • 什么是爬虫?——从技术原理到现实应用的全面解析 III
  • HTTP 请求头与请求体:数据存储的底层逻辑与实践指南
  • leetcode day36 01背包问题 494
  • react 父子组件通信 子 直接到父, 父 forwardref子
  • 基于扣子(Coze.cn)与火山引擎构建高性能智能体的实践指南
  • 【Linux网络与网络编程】07.应用层协议HTTPS