当前位置:   article > 正文

SentenceTransformers 库介绍_sentence transformer model.encode

sentence transformer model.encode

SentenceTransformers 是一个可以用于句子、文本和图像嵌入的Python库。 可以为 100 多种语言计算文本的嵌入并且可以轻松地将它们用于语义文本相似性、语义搜索和同义词挖掘等常见任务。该框架基于 PyTorch 和 Transformers,并提供了大量针对各种任务的预训练模型。 还可以很容易根据自己的模型进行微调。

  1. ### 1. install
  2. pip install -U sentence-transformers
  3. ### 2. Computing Sentence Embeddings
  4. from sentence_transformers import SentenceTransformer
  5. model = SentenceTransformer('all-MiniLM-L6-v2')
  6. #Our sentences we like to encode
  7. sentences = ['This framework generates embeddings for each input sentence',
  8. 'Sentences are passed as a list of string.',
  9. 'The quick brown fox jumps over the lazy dog.']
  10. #Sentences are encoded by calling model.encode()
  11. embeddings = model.encode(sentences)
  12. #Print the embeddings
  13. for sentence, embedding in zip(sentences, embeddings):
  14. print("Sentence:", sentence)
  15. print("Embedding:", embedding)
  16. print("")
  17. # 注: 网络连接问题,导致模型下载失败!
  18. ### 3. Semantic Textual Similarity
  19. from sentence_transformers import SentenceTransformer, util
  20. model = SentenceTransformer('all-MiniLM-L6-v2')
  21. # Two lists of sentences
  22. sentences1 = ['The cat sits outside',
  23. 'A man is playing guitar',
  24. 'The new movie is awesome']
  25. sentences2 = ['The dog plays in the garden',
  26. 'A woman watches TV',
  27. 'The new movie is so great']
  28. #Compute embedding for both lists
  29. embeddings1 = model.encode(sentences1, convert_to_tensor=True)
  30. embeddings2 = model.encode(sentences2, convert_to_tensor=True)
  31. #Compute cosine-similarities
  32. cosine_scores = util.cos_sim(embeddings1, embeddings2)
  33. #Output the pairs with their score
  34. for i in range(len(sentences1)):
  35. print("{} \t\t {} \t\t Score: {:.4f}".format(sentences1[i], sentences2[i], cosine_scores[i][i]))

参考:

https://www.sbert.net/examples/applications/computing-embeddings/README.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/367601
推荐阅读
相关标签
  

闽ICP备14008679号