许可证: mit
语言:
- 俄语
- 英语
任务标签: 句子相似度
标签:
- 俄语
- 填充掩码
- 预训练
- 嵌入向量
- 掩码语言模型
- 微型模型
- 特征提取
- 句子相似度
- 句子转换器
- 转换器
小部件示例:
SciRus-tiny 是一个用于获取俄语和英语科学文本嵌入向量的模型。该模型基于 eLibrary 数据训练,采用了 habr 文章 中描述的对比学习技术。在 ruSciBench 基准测试中取得了较高的指标值。
如何获取嵌入向量
from transformers import AutoTokenizer, AutoModel
import torch.nn.functional as F
import torch
tokenizer = AutoTokenizer.from_pretrained("mlsa-iai-msu-lab/sci-rus-tiny")
model = AutoModel.from_pretrained("mlsa-iai-msu-lab/sci-rus-tiny")
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0]
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
def get_sentence_embedding(title, abstract, model, tokenizer, max_length=None):
sentence = '</s>'.join([title, abstract])
encoded_input = tokenizer(
[sentence], padding=True, truncation=True, return_tensors='pt', max_length=max_length).to(model.device)
with torch.no_grad():
model_output = model(**encoded_input)
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
return sentence_embeddings.cpu().detach().numpy()[0]
print(get_sentence_embedding('标题示例', '摘要示例', model, tokenizer).shape)
或者可以使用 sentence_transformers
库:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('mlsa-iai-msu-lab/sci-rus-tiny')
embeddings = model.encode(['示例标题' + '</s>' + '示例摘要'])
print(embeddings[0].shape)
作者
本基准测试由莫斯科国立大学人工智能研究所的 MLSA 实验室开发。
致谢
本研究隶属于莫斯科国立大学 SES 项目 #23-Ш05-21 "开发用于处理大规模文本科学信息的机器学习数学方法"。感谢 eLibrary 提供的数据集。
联系方式
Nikolai Gerasimenko (nikgerasimenko@gmail.com), Alexey Vatolin (vatolinalex@gmail.com)
引用
@article{Gerasimenko2024,
author = {Gerasimenko, N. and Vatolin, A. and Ianina, A. and Vorontsov, K.},
title = {SciRus: Tiny and Powerful Multilingual Encoder for Scientific Texts},
journal = {Doklady Mathematics},
year = {2024},
volume = {110},
number = {1},
pages = {S193--S202},
month = {dec},
issn = {1531-8362},
doi = {10.1134/S1064562424602178},
url = {https://doi.org/10.1134/S1064562424602178}
}