pytorch安裝參考https://blog.csdn.net/sinat_41563673/article/details/97402059
https://blog.csdn.net/excellent_sun/article/details/88830687
1、anaconda3安裝方法見https://blog.csdn.net/qq_42412214/article/details/94917609
2、創建名爲torch的anaconda虛擬環境:
conda create -n torch python=3.6.5
3、安裝pytorch
1)添加anaconda國內源,使anaconda安裝環境更快
將以下配置文件寫在~/.condarc中(初始爲空文件)
sudo gedit ~/.condarc
channels:
- https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
- https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
- defaults
show_channel_urls: true
2)在pytorch官網找到對應版本安裝命令https://pytorch.org/get-started/locally/
選擇本機CUDA10.0 conda安裝的版本
# CUDA 10.0
conda install pytorch==1.2.0 torchvision==0.4.0 cudatoolkit=10.0 -c pytorch
3)進入conda虛擬環境torch,使用上述命令安裝
4)安裝成功後import torch,沒有報錯
5)繼續測試
python tor.py
python torgpu.py
tor.py代碼:
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 4 14:26:59 2018
@author: admin
"""
import torch #引用torch包
x = torch.Tensor(2,3)
print(x)
torgpu.py代碼:
# CUDA TEST
import torch
x = torch.Tensor([1.0])
xx = x.cuda()
print(xx)
# CUDNN TEST
from torch.backends import cudnn
print(cudnn.is_acceptable(xx))