pytorch的安裝 基於anaconda

pytorch的安裝 基於anaconda

爲anaconda添加源


注:下載pytorch之前最好先新建一個虛擬環境,下面的操作將都在虛擬環境中進行,如果發生錯誤也不會影響到整臺電腦

點擊create創建虛擬環境

在這裏插入圖片描述

選擇python版本,爲虛擬環境取名字

在這裏插入圖片描述

點擊Open Terminal進入命令行

在這裏插入圖片描述

添加 源 (防止下載超時,加速下載)

# 添加清華源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
# 添加第三方conda源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

官網選擇版本

點擊此處進入官網
進入官網之後下拉 找到了之後依次選擇想要的版本
選擇1.4(這個版本較穩定),接下來根據各人的操作系統和Python版本以及CUDA選擇對應的版本
我們選擇conda命令(當然也可以用pip)
在這裏插入圖片描述

conda install pytorch torchvision cudatoolkit=10.1 -c pytorch

這是官網給我們的命令,爲了使用清華的源,我們在下載的時候去掉-c pytorch 即可
在新建的虛擬環境中輸入下面的命令,就開始自動下載了

conda install pytorch torchvision cudatoolkit=10.1 

檢測是否成功

這裏運行pycharm,我選擇新建一個工程專門用來pytorch的學習
注意:python環境的選擇要與自己建立的虛擬環境目錄相對應
在這裏插入圖片描述
可以看到我們已經成功安裝了pytorch
簡單的輸入幾條語句測試一下

import torch
print(torch.__version__)
print('gpu:',torch.cuda.is_available())

在這裏插入圖片描述
順利返回了版本信息,以及支持gpu的True

成功!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章