安装sentence-transformers

点击此处可访问SBERT官方代码(GitHub)

在安装sentence-transformers之前需要确保以下条件:
We recommend Python 3.6 or higher, PyTorch 1.6.0 or higher and transformers v4.6.0 or higher. The code does not work with Python 2.7.

一、安装PyTorch

点击此处可访问PyTorch官网
点击此处可查看PyTorch历史版本安装说明

pip install torch===1.7.0 torchvision===0.8.1 torchaudio===0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

执行命令后的运行情况大致如下:

Looking in links: https://download.pytorch.org/whl/torch_stable.html
Collecting torch===1.7.0
  Using cached torch-1.7.0-cp36-none-macosx_10_9_x86_64.whl (108.0 MB)
Collecting torchvision===0.8.1
  Using cached torchvision-0.8.1-cp36-cp36m-macosx_10_9_x86_64.whl (1.0 MB)
Collecting torchaudio===0.7.0
  Downloading torchaudio-0.7.0-cp36-cp36m-macosx_10_9_x86_64.whl (1.4 MB)
     |████████████████████████████████| 1.4 MB 32 kB/s             
Collecting dataclasses
  Using cached dataclasses-0.8-py3-none-any.whl (19 kB)
Requirement already satisfied: typing-extensions in /anaconda/lib/python3.6/site-packages (from torch===1.7.0) (3.7.4.3)
Requirement already satisfied: numpy in /anaconda/lib/python3.6/site-packages (from torch===1.7.0) (1.19.5)
Requirement already satisfied: future in /anaconda/lib/python3.6/site-packages (from torch===1.7.0) (0.17.1)
Requirement already satisfied: pillow>=4.1.1 in /anaconda/lib/python3.6/site-packages (from torchvision===0.8.1) (4.1.1)
Requirement already satisfied: olefile in /anaconda/lib/python3.6/site-packages (from pillow>=4.1.1->torchvision===0.8.1) (0.44)
Installing collected packages: dataclasses, torch, torchvision, torchaudio
  Attempting uninstall: torch
    Found existing installation: torch 1.3.1
    Uninstalling torch-1.3.1:
      Successfully uninstalled torch-1.3.1
Successfully installed dataclasses-0.8 torch-1.7.0 torchaudio-0.7.0 torchvision-0.8.1

二、安装transformers

点击此处可访问transformers官网,可查看其安装、使用、历史版本

若直接执行pip install transformers会报错如下:

Building wheels for collected packages: tokenizers
  Building wheel for tokenizers (pyproject.toml) ... error
  ERROR: Command errored out with exit status 1:
   command: /anaconda/bin/python /anaconda/lib/python3.6/site-packages/pip/_vendor/pep517/in_process/_in_process.py build_wheel /var/folders/4r/71h2vznj3ssgkgvt2_38rylm0000gn/T/tmp5fwt5auo
       cwd: /private/var/folders/4r/71h2vznj3ssgkgvt2_38rylm0000gn/T/pip-install-63wkdddk/tokenizers_5ce428d4037b4551b1a7deb0d23b22ed
  Complete output (51 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.macosx-10.7-x86_64-3.6
  creating build/lib.macosx-10.7-x86_64-3.6/tokenizers
  copying py_src/tokenizers/__init__.py -> build/lib.macosx-10.7-x86_64-3.6/tokenizers
  creating build/lib.macosx-10.7-x86_64-3.6/tokenizers/models
  copying py_src/tokenizers/models/__init__.py -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/models
  creating build/lib.macosx-10.7-x86_64-3.6/tokenizers/decoders
  copying py_src/tokenizers/decoders/__init__.py -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/decoders
  creating build/lib.macosx-10.7-x86_64-3.6/tokenizers/normalizers
  copying py_src/tokenizers/normalizers/__init__.py -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/normalizers
  creating build/lib.macosx-10.7-x86_64-3.6/tokenizers/pre_tokenizers
  copying py_src/tokenizers/pre_tokenizers/__init__.py -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/pre_tokenizers
  creating build/lib.macosx-10.7-x86_64-3.6/tokenizers/processors
  copying py_src/tokenizers/processors/__init__.py -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/processors
  creating build/lib.macosx-10.7-x86_64-3.6/tokenizers/trainers
  copying py_src/tokenizers/trainers/__init__.py -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/trainers
  creating build/lib.macosx-10.7-x86_64-3.6/tokenizers/implementations
  copying py_src/tokenizers/implementations/byte_level_bpe.py -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/implementations
  copying py_src/tokenizers/implementations/sentencepiece_unigram.py -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/implementations
  copying py_src/tokenizers/implementations/sentencepiece_bpe.py -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/implementations
  copying py_src/tokenizers/implementations/base_tokenizer.py -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/implementations
  copying py_src/tokenizers/implementations/__init__.py -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/implementations
  copying py_src/tokenizers/implementations/char_level_bpe.py -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/implementations
  copying py_src/tokenizers/implementations/bert_wordpiece.py -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/implementations
  creating build/lib.macosx-10.7-x86_64-3.6/tokenizers/tools
  copying py_src/tokenizers/tools/__init__.py -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/tools
  copying py_src/tokenizers/tools/visualizer.py -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/tools
  copying py_src/tokenizers/__init__.pyi -> build/lib.macosx-10.7-x86_64-3.6/tokenizers
  copying py_src/tokenizers/models/__init__.pyi -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/models
  copying py_src/tokenizers/decoders/__init__.pyi -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/decoders
  copying py_src/tokenizers/normalizers/__init__.pyi -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/normalizers
  copying py_src/tokenizers/pre_tokenizers/__init__.pyi -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/pre_tokenizers
  copying py_src/tokenizers/processors/__init__.pyi -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/processors
  copying py_src/tokenizers/trainers/__init__.pyi -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/trainers
  copying py_src/tokenizers/tools/visualizer-styles.css -> build/lib.macosx-10.7-x86_64-3.6/tokenizers/tools
  running build_ext
  running build_rust
  error: can't find Rust compiler
  
  If you are using an outdated pip version, it is possible a prebuilt wheel is available for this package but pip is not able to install from it. Installing from the wheel would avoid the need for a Rust compiler.
  
  To update pip, run:
  
      pip install --upgrade pip
  
  and then retry package installation.
  
  If you did intend to build this package from source, try installing a Rust compiler from your system package manager and ensure it is on the PATH during installation. Alternatively, rustup (available at https://rustup.rs) is the recommended way to download and update the Rust compiler toolchain.
  ----------------------------------------
  ERROR: Failed building wheel for tokenizers
Failed to build tokenizers
ERROR: Could not build wheels for tokenizers, which is required to install pyproject.toml-based projects

最终通过pip install transformers==4.7.0成功安装transformers,即指定了特定版本

执行命令后的运行情况大致如下:

Collecting transformers==4.7.0
  Downloading transformers-4.7.0-py3-none-any.whl (2.5 MB)
     |████████████████████████████████| 2.5 MB 28 kB/s            
Requirement already satisfied: pyyaml in /anaconda/lib/python3.6/site-packages (from transformers==4.7.0) (3.12)
Requirement already satisfied: importlib-metadata in /anaconda/lib/python3.6/site-packages (from transformers==4.7.0) (3.10.0)
Collecting sacremoses
  Using cached sacremoses-0.0.47-py2.py3-none-any.whl (895 kB)
Requirement already satisfied: packaging in /anaconda/lib/python3.6/site-packages (from transformers==4.7.0) (16.8)
Collecting tqdm>=4.27
  Using cached tqdm-4.62.3-py2.py3-none-any.whl (76 kB)
Requirement already satisfied: dataclasses in /anaconda/lib/python3.6/site-packages (from transformers==4.7.0) (0.8)
Collecting tokenizers<0.11,>=0.10.1
  Downloading tokenizers-0.10.3-cp36-cp36m-macosx_10_11_x86_64.whl (2.2 MB)
     |████████████████████████████████| 2.2 MB 15 kB/s            
Requirement already satisfied: numpy>=1.17 in /anaconda/lib/python3.6/site-packages (from transformers==4.7.0) (1.19.5)
Collecting huggingface-hub==0.0.8
  Downloading huggingface_hub-0.0.8-py3-none-any.whl (34 kB)
Collecting filelock
  Using cached filelock-3.4.1-py3-none-any.whl (9.9 kB)
Collecting regex!=2019.12.17
  Using cached regex-2022.1.18-cp36-cp36m-macosx_10_9_x86_64.whl (289 kB)
Requirement already satisfied: requests in /anaconda/lib/python3.6/site-packages (from transformers==4.7.0) (2.25.1)
Requirement already satisfied: typing-extensions>=3.6.4 in /anaconda/lib/python3.6/site-packages (from importlib-metadata->transformers==4.7.0) (3.7.4.3)
Requirement already satisfied: zipp>=0.5 in /anaconda/lib/python3.6/site-packages (from importlib-metadata->transformers==4.7.0) (3.4.1)
Requirement already satisfied: idna<3,>=2.5 in /anaconda/lib/python3.6/site-packages (from requests->transformers==4.7.0) (2.5)
Requirement already satisfied: certifi>=2017.4.17 in /anaconda/lib/python3.6/site-packages (from requests->transformers==4.7.0) (2020.12.5)
Requirement already satisfied: chardet<5,>=3.0.2 in /anaconda/lib/python3.6/site-packages (from requests->transformers==4.7.0) (3.0.3)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /anaconda/lib/python3.6/site-packages (from requests->transformers==4.7.0) (1.26.4)
Requirement already satisfied: click in /anaconda/lib/python3.6/site-packages (from sacremoses->transformers==4.7.0) (6.7)
Requirement already satisfied: joblib in /anaconda/lib/python3.6/site-packages (from sacremoses->transformers==4.7.0) (0.11)
Requirement already satisfied: six in /anaconda/lib/python3.6/site-packages (from sacremoses->transformers==4.7.0) (1.12.0)
Installing collected packages: tqdm, regex, filelock, tokenizers, sacremoses, huggingface-hub, transformers
  Attempting uninstall: tqdm
    Found existing installation: tqdm 4.19.9
    Uninstalling tqdm-4.19.9:
      Successfully uninstalled tqdm-4.19.9
Successfully installed filelock-3.4.1 huggingface-hub-0.0.8 regex-2022.1.18 sacremoses-0.0.47 tokenizers-0.10.3 tqdm-4.62.3 transformers-4.7.0

三、安装sentence-transformers

pip install sentence-transformers

执行命令后的运行情况大致如下:

Collecting sentence-transformers
  Using cached sentence_transformers-2.2.0-py3-none-any.whl
Requirement already satisfied: tqdm in /anaconda/lib/python3.6/site-packages (from sentence-transformers) (4.62.3)
Requirement already satisfied: scipy in /anaconda/lib/python3.6/site-packages (from sentence-transformers) (1.2.1)
Requirement already satisfied: torchvision in /anaconda/lib/python3.6/site-packages (from sentence-transformers) (0.8.1)
Requirement already satisfied: numpy in /anaconda/lib/python3.6/site-packages (from sentence-transformers) (1.19.5)
Requirement already satisfied: transformers<5.0.0,>=4.6.0 in /anaconda/lib/python3.6/site-packages (from sentence-transformers) (4.7.0)
Requirement already satisfied: huggingface-hub in /anaconda/lib/python3.6/site-packages (from sentence-transformers) (0.0.8)
Requirement already satisfied: nltk in /anaconda/lib/python3.6/site-packages (from sentence-transformers) (3.2.3)
Requirement already satisfied: torch>=1.6.0 in /anaconda/lib/python3.6/site-packages (from sentence-transformers) (1.7.0)
Collecting sentencepiece
  Using cached sentencepiece-0.1.96-cp36-cp36m-macosx_10_6_x86_64.whl (1.2 MB)
Requirement already satisfied: scikit-learn in /anaconda/lib/python3.6/site-packages (from sentence-transformers) (0.19.1)
Requirement already satisfied: future in /anaconda/lib/python3.6/site-packages (from torch>=1.6.0->sentence-transformers) (0.17.1)
Requirement already satisfied: dataclasses in /anaconda/lib/python3.6/site-packages (from torch>=1.6.0->sentence-transformers) (0.8)
Requirement already satisfied: typing-extensions in /anaconda/lib/python3.6/site-packages (from torch>=1.6.0->sentence-transformers) (3.7.4.3)
Requirement already satisfied: regex!=2019.12.17 in /anaconda/lib/python3.6/site-packages (from transformers<5.0.0,>=4.6.0->sentence-transformers) (2022.1.18)
Requirement already satisfied: sacremoses in /anaconda/lib/python3.6/site-packages (from transformers<5.0.0,>=4.6.0->sentence-transformers) (0.0.47)
Requirement already satisfied: importlib-metadata in /anaconda/lib/python3.6/site-packages (from transformers<5.0.0,>=4.6.0->sentence-transformers) (3.10.0)
Requirement already satisfied: tokenizers<0.11,>=0.10.1 in /anaconda/lib/python3.6/site-packages (from transformers<5.0.0,>=4.6.0->sentence-transformers) (0.10.3)
Requirement already satisfied: pyyaml in /anaconda/lib/python3.6/site-packages (from transformers<5.0.0,>=4.6.0->sentence-transformers) (3.12)
Requirement already satisfied: packaging in /anaconda/lib/python3.6/site-packages (from transformers<5.0.0,>=4.6.0->sentence-transformers) (16.8)
Requirement already satisfied: filelock in /anaconda/lib/python3.6/site-packages (from transformers<5.0.0,>=4.6.0->sentence-transformers) (3.4.1)
Requirement already satisfied: requests in /anaconda/lib/python3.6/site-packages (from transformers<5.0.0,>=4.6.0->sentence-transformers) (2.25.1)
Requirement already satisfied: six in /anaconda/lib/python3.6/site-packages (from nltk->sentence-transformers) (1.12.0)
Requirement already satisfied: pillow>=4.1.1 in /anaconda/lib/python3.6/site-packages (from torchvision->sentence-transformers) (4.1.1)
Requirement already satisfied: olefile in /anaconda/lib/python3.6/site-packages (from pillow>=4.1.1->torchvision->sentence-transformers) (0.44)
Requirement already satisfied: zipp>=0.5 in /anaconda/lib/python3.6/site-packages (from importlib-metadata->transformers<5.0.0,>=4.6.0->sentence-transformers) (3.4.1)
Requirement already satisfied: idna<3,>=2.5 in /anaconda/lib/python3.6/site-packages (from requests->transformers<5.0.0,>=4.6.0->sentence-transformers) (2.5)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /anaconda/lib/python3.6/site-packages (from requests->transformers<5.0.0,>=4.6.0->sentence-transformers) (1.26.4)
Requirement already satisfied: certifi>=2017.4.17 in /anaconda/lib/python3.6/site-packages (from requests->transformers<5.0.0,>=4.6.0->sentence-transformers) (2020.12.5)
Requirement already satisfied: chardet<5,>=3.0.2 in /anaconda/lib/python3.6/site-packages (from requests->transformers<5.0.0,>=4.6.0->sentence-transformers) (3.0.3)
Requirement already satisfied: click in /anaconda/lib/python3.6/site-packages (from sacremoses->transformers<5.0.0,>=4.6.0->sentence-transformers) (6.7)
Requirement already satisfied: joblib in /anaconda/lib/python3.6/site-packages (from sacremoses->transformers<5.0.0,>=4.6.0->sentence-transformers) (0.11)
Installing collected packages: sentencepiece, sentence-transformers
Successfully installed sentence-transformers-2.2.0 sentencepiece-0.1.96
?著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,100评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,308评论 3 388
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事?!?“怎么了?”我有些...
    开封第一讲书人阅读 159,718评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,275评论 1 287
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,376评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,454评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,464评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,248评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,686评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,974评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,150评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,817评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,484评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,140评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,374评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,012评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,041评论 2 351

推荐阅读更多精彩内容