windows10上conda安装pytorch+transformers

windows10上conda安装pytorch+transformers

  • conda创建环境
  • 安装pytorch
    • CUDA 11.3
    • CPU Only
    • 验证
  • 安装transformers
    • 安装报错
    • 解决办法
    • 验证

conda创建环境

conda create -n myPytorch python=3.7

安装pytorch

CUDA 11.3

conda install pytorch torchvision torchaudio cudatoolkit==11.3 -c pytorch

CPU Only

conda install pytorch torchvision torchaudio cpuonly -c pytorch

或指定版本(比如v1.8.1)

conda install pytorch == 1.8.1 torchvision == 0.9.1 torchaudio==0.8.1 cpuonly -c pytorch

验证

import torch
x = torch.rand(5, 3)
print(x)

输出如下则安装成功

tensor([[0.3380, 0.3845, 0.3217],
[0.8337, 0.9050, 0.2650],
[0.2979, 0.7141, 0.9069],
[0.1449, 0.1132, 0.1375],
[0.4675, 0.3947, 0.1426]])

验证GPU和CUDA是否可用

import torch
torch.cuda.is_available()

安装transformers

pip install transformers

安装报错

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

解决办法

conda install -c huggingface transformers

这种方法能够成功安装,但会降低自己numpy等的版本,导致后续出现其他问题

pip install transformers==4.4.1

最后是指定特定版本安装即可

验证

from transformers import pipeline
classifier= pipeline(‘sentiment-analysis’)
classifier(‘we love you’)

输出

[{‘label’: ‘POSITIVE’, ‘score’: 0.9998704791069031}]

你可能感兴趣的:(机器学习环境,pytorch,深度学习,python,transformer)