conda安装pytorch

conda安装pytorch

    • 说明
    • anaconda版本
    • windows10安装cuda10.2
    • 配置国内清华镜像源
    • 切换到pytorch环境
    • 安装到当前环境
    • 验证
    • 测试对比cpu和gpu

说明

  1. 使用conda安装,不建议使用pip安装;
  2. cuda使用10.0或更高版本.

anaconda版本

Anaconda3-2019.10-Windows-x86_64.exe

windows10安装cuda10.2

windows10安装cuda10.2

配置国内清华镜像源

https://blog.csdn.net/bingo_liu/article/details/103224105

切换到pytorch环境

conda activate pytorch

安装到当前环境

conda install pytorch==1.2.0 torchvision==0.4.0 cudatoolkit=10.0 -c pytorch

**提示:**虽然使用了国内源,但是安装过程中提示错误:

CondaHTTPError: HTTP 000 CONNECTION FAILED for url 
Elapsed: -

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.

重试几次或十来次或会成功

验证

(pytorch) C:\Users\Administrator>python
Python 3.7.5 (default, Oct 31 2019, 15:18:51) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> print(torch.__version__)
1.2.0

测试对比cpu和gpu

# coding=utf-8
import torch
from time import perf_counter

x = torch.rand(10000, 100000)
y = torch.rand(100000, 10000)

# CPU
start = perf_counter()
x.mm(y)
finish = perf_counter()
time = finish - start
print("CPU计算时间:%s" % time)

# GPU
if torch.cuda.is_available():
    x = x.cuda()
    y = y.cuda()
    start = perf_counter()
    x.mm(y)
    finish = perf_counter()
    time_cuda = finish - start
    print("GPU加速计算的时间:%s" % time_cuda)
    print("CPU计算时间是GPU加速计算时间的%s倍" % str(time / time_cuda))

else:
    print("未支持CUDA")

输出

# CPU计算时间:108.6555498
# GPU加速计算的时间:0.21388500000000477
# CPU计算时间是GPU加速计算时间的508.0092096219818倍

如果机子内存不够,请将矩阵x的列数和y的行数改小点,哈哈

你可能感兴趣的:(pytorch)