Pytorch简明笔记1——概况与环境搭建

给你一囗甜甜゛ 提交于 2020-02-27 06:01:59

去年年初,本来立志2019一定勤勤恳恳写博客,然而。。。

今年决定就算写《从入门到放弃》也要养成某种时刻落地日常积累的习惯。

作为一个主用keras/TF的算法码农,决定在新春伊始,对pytorch说一声真香,与其争论哪一种开发工具未来能一统天下,不如自己两种都能熟练掌握。所以,作为一个pytorch小白,在这肺炎疫情赏赐了的多三天假期内,决定开始自学pytorch,算是给自己标榜的全栈工程师目标再加上一块技能拼图。

 

What  is Pytorch

pytorch是一个python机器学习库,底层是C++实现,所以执行效率没的说。facebook开发,多个大公司如 Uber 、摩根大通等背书,在人工智能领域不乏成功案例。更重要的是,在最近AI领域的几大顶会中,2019年的pytorch的使用数量激增,已经全面超过了tf、caffe2、theano等诸君(不过tf2那时还为正式发布和普及)

 

pytorch比tensorflow更简洁(咳咳。。keras表示不服)

例如,实现如下这个简单的计算图

tensorflow代码实现

tensorflow把构造计算图和运算分开处理,即如果想做上面图中的运算,需要先把上面这个计算图用tensorflow构建出来,然后才能进行运算。


import numpy
import tensorflow

numpy.random.seed(0)

N, D = 3, 4

# 第一步:定义计算图
# 1.1 定义占位符: 定义变量时是不能给它们赋值的,必须是空的,所以也叫占位符。
x = tensorflow.placeholder(tensorflow.float32)
y = tensorflow.placeholder(tensorflow.float32)
z = tensorflow.placeholder(tensorflow.float32)

# 1.2定义运算
a = x * y
b = a + z
c = tensorflow.reduce_sum(b)

# 1.3 定义梯度:应用在反向传播中
grad_x, grad_y, grad_z = tensorflow.gradients(c, [x, y, z])

# 第二步:计算
# TensorFlow中的计算是用会话模型进行的
# 2.1 开启会话
with tensorflow.Session() as session:
    # 2.2对占位符注入数据
    value = {
        x: numpy.random.randn(N, D),
        y: numpy.random.randn(N, D),
        z: numpy.random.randn(N, D)
    }

    # 2.3对有数据的占位符进行运算
    out = session.run([c, grad_x, grad_y, grad_z], feed_dict=value)
    c_value, grad_x_value, grad_y_value, grad_z_value = out

这里注意:可能会遇到依赖包版本过低的问题,请升级protobuf至少到3.6.1

pip install protobuf==3.6.1

(本文出自oschina博主happyBKs的博文:https://my.oschina.net/happyBKs/blog/3162262,转载请注明出处)

pytorch代码实现

pytorch在定义占位符的同时可以进行初始化,然后直接进行运算即可。

import torch

N, D = 3, 4

# 定义变量,定义的同时可以赋值初始化
x = torch.tensor(torch.rand(N, D), requires_grad=True)
y = torch.tensor(torch.rand(N, D), requires_grad=True)
z = torch.tensor(torch.rand(N, D), requires_grad=True)

# 进行运算
a = x * y
b = a + z
c = torch.sum(b)

c.backward()

 

PyTorch和TensorFlow的不同

总结一下PyTorch和TensorFlow的不同:

这里最大的区别是第一项计算图的处理方法,pytorch是动态计算图,TensorFlow是金泰计算图。所谓动态,指计算的时候计算图和计算同时进行;静态是指需要提前计算图,然后进行计算。

 

安装pytorch

你可以用命令安装。也可以将文件下载下来安装。从官网上找即可。

这里讲历史版本的两种方式的寻找方法给出了来:

如果你要找所有版本的环境的wheel文件,请在这个地址中找:

https://download.pytorch.org/whl/torch_stable.html

旧版本的各个安装命令:

https://pytorch.org/get-started/previous-versions/

如果你网速很差,不妨考虑将wheel文件用迅雷下载下来,然后pip install本地wheel文件。注意,有torch-1.4.0+cpu-cp36-cp36m-win_amd64.whl和torchvision-0.5.0+cpu-cp36-cp36m-win_amd64.whl两个文件。

PS:我的环境是Python3.6(需要从3.6.0升级到3.6.7)

本地文件安装方式:

(D:\ProgramData\Anaconda3) C:\Users\Neil\PycharmProjects\Python123>pip install D:/install/pytorch/torch-1.4.0+cpu-cp36-cp36m-win_amd64.whl
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Processing d:\install\pytorch\torch-1.4.0+cpu-cp36-cp36m-win_amd64.whl
pillow 4.0.0 requires olefile, which is not installed.
tensorflow 1.12.0 has requirement protobuf>=3.6.1, but you'll have protobuf 3.6.0 which is incompatible.
Installing collected packages: torch
Successfully installed torch-1.4.0+cpu
You are using pip version 10.0.1, however version 20.0.2 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

 


conda安装方式:(推荐,很多依赖包,所以这种方式比pip更好)

(D:\ProgramData\Anaconda3) C:\Users\Neil\PycharmProjects\Python123>conda install pytorch torchvision cpuonly -c pytorch
Fetching package metadata .......................
Solving package specifications: .

Package plan for installation in environment D:\ProgramData\Anaconda3:

The following NEW packages will be INSTALLED:

    blas:              1.0-mkl                 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    cpuonly:           1.0-0                   pytorch
    icc_rt:            2019.0.0-h0cc432a_1     https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    intel-openmp:      2019.4-245              https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    mkl_fft:           1.0.6-py36hdbbee80_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    mkl_random:        1.0.1-py36h77b88f5_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    msys2-conda-epoch: 20160418-1              defaults
    ninja:             1.7.2-0                 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    numpy-base:        1.15.4-py36h8128ebf_0   https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    olefile:           0.44-py36_0             https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    pytorch:           1.4.0-py3.6_cpu_0       pytorch                                                 [cpuonly]
    tbb:               2018.0.5-he980bc4_0     https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    tbb4py:            2018.0.5-py36he980bc4_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    torchvision:       0.5.0-py36_cpu          pytorch                                                 [cpuonly]
    vc:                14-0                    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free

The following packages will be UPDATED:

    conda:             4.3.8-py36_0            defaults                                                --> 4.3.30-py36h7e176b0_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    mkl:               2017.0.1-0              defaults                                                --> 2018.0.3-1            https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    numexpr:           2.6.1-np111py36_2       defaults                                                --> 2.6.8-py36h9ef55f4_0  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    numpy:             1.11.3-py36_0           defaults                                                --> 1.15.4-py36ha559c80_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    pillow:            4.0.0-py36_0            defaults                                                --> 4.2.1-py36_0          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    scikit-learn:      0.18.1-np111py36_1      defaults                                                --> 0.20.0-py36heebcf9a_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    scipy:             0.18.1-np111py36_1      defaults                                                --> 1.1.0-py36h4f6bf74_1  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main

The following packages will be SUPERCEDED by a higher-priority channel:

    conda-env:         2.6.0-0                 defaults                                                --> 2.6.0-0               https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free

Proceed ([y]/n)? y

blas-1.0-mkl.t 100% |###############################| Time: 0:00:00   0.00  B/s
conda-env-2.6. 100% |###############################| Time: 0:00:00  31.88 kB/s
cpuonly-1.0-0. 100% |###############################| Time: 0:00:00 128.39 kB/s
icc_rt-2019.0. 100% |###############################| Time: 0:00:00  11.90 MB/s
intel-openmp-2 100% |###############################| Time: 0:00:00  38.20 MB/s
msys2-conda-ep 100% |###############################| Time: 0:00:00 132.53 kB/s
mkl-2018.0.3-1 100% |###############################| Time: 0:00:26   6.96 MB/s
ninja-1.7.2-0. 100% |###############################| Time: 0:00:00   9.11 MB/s
vc-14-0.tar.bz 100% |###############################| Time: 0:00:00  45.08 kB/s
tbb-2018.0.5-h 100% |###############################| Time: 0:00:00  10.92 MB/s
olefile-0.44-p 100% |###############################| Time: 0:00:00   3.41 MB/s
tbb4py-2018.0. 100% |###############################| Time: 0:00:00   0.00  B/s
numpy-base-1.1 100% |###############################| Time: 0:00:00   8.91 MB/s
pillow-4.2.1-p 100% |###############################| Time: 0:00:00  10.61 MB/s
conda-4.3.30-p 100% |###############################| Time: 0:00:00   9.87 MB/s
mkl_fft-1.0.6- 100% |###############################| Time: 0:00:00   7.89 MB/s
mkl_random-1.0 100% |###############################| Time: 0:00:00  17.58 MB/s
numpy-1.15.4-p 100% |###############################| Time: 0:00:00   2.36 MB/s
numexpr-2.6.8- 100% |###############################| Time: 0:00:00   9.40 MB/s
pytorch-1.4.0- 100% |###############################| Time: 0:54:19  19.91 kB/s
scipy-1.1.0-py 100% |###############################| Time: 0:00:00  15.55 MB/s
scikit-learn-0 100% |###############################| Time: 0:00:00   7.54 MB/s
torchvision-0. 100% |###############################| Time: 0:00:02   2.59 MB/s

(D:\ProgramData\Anaconda3) C:\Users\Neil\PycharmProjects\Python123>

 

但是,我运行代码出现以下错误:

from torch._C import * 
ImportError: DLL load failed: 找不到指定的程序。

网上不少人也遇到相同的问题。

但是造成这个错误的原因可能有多种,各类原因这里给大家:

1)python版本要升级,我试验成功的是3.6.7。如果你是3.6.0,请一定升级,否则会报这个错。(我PC环境遇到的问题)

2)numpy版本更新到比较新的版本。

3)windows环境下安装VC++ 2017库。(这个我在调试环境过程中安装了,但不清楚是否是必要的。只是参考了部分网友的博文,说是原因之一,这里也列举出来,遇到问题(1)和(2)如果不能解决,记得试一试)

我这里遇到的主要原因是python版本3.6.0需要升级到3.6.7:

(D:\ProgramData\Anaconda3) C:\Users\Neil\PycharmProjects\Python123>conda install python=3.6.7
Fetching package metadata .......................
Solving package specifications: .

Package plan for installation in environment D:\ProgramData\Anaconda3:

The following NEW packages will be INSTALLED:

    ca-certificates: 2019.11.27-0      https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    krb5:            1.13.2-0          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    libcurl:         7.62.0-h2a8f88b_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    libssh2:         1.8.0-hd619d38_4  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    sqlite:          3.30.1-he774522_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    xz:              5.2.4-h2fa13f4_4  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    zstd:            1.3.7-h508b16e_0  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main

The following packages will be UPDATED:

    freetype:        2.5.5-vc14_2      defaults                                                --> 2.9.1-ha9979f8_1        https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    icu:             57.1-vc14_0       defaults                                                --> 58.2-ha66f8fd_1         https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    jpeg:            9b-vc14_0         defaults                                                --> 9b-hb83a4c4_2           https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    libpng:          1.6.27-vc14_0     defaults                                                --> 1.6.37-h2a8f88b_0       https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    libtiff:         4.0.6-vc14_3      defaults                                                --> 4.1.0-h56a325e_0        https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    openssl:         1.0.2k-vc14_0     defaults                                                --> 1.0.2u-he774522_0       https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    pillow:          4.2.1-py36_0      https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free --> 7.0.0-py36hcc1f983_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    pycurl:          7.43.0-py36_2     defaults                                                --> 7.43.0.2-py36h74b6da3_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    python:          3.6.0-0           defaults                                                --> 3.6.7-h9f7ef89_2        https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    qt:              5.6.2-vc14_3      defaults                                                --> 5.6.2-vc14h6f8c307_12   https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    tk:              8.5.18-vc14_0     defaults                                                --> 8.6.8-hfa6e2cd_0        https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    vc:              14-0              https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free --> 14.1-h0510ff6_4         https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    vs2015_runtime:  14.0.25123-0      defaults                                                --> 14.16.27012-hf0eaf9b_1  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    zlib:            1.2.8-vc14_3      defaults                                                --> 1.2.11-h62dcd97_3       https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main

The following packages will be SUPERSEDED by a higher-priority channel:

    bzip2:           1.0.6-vc14_3      defaults                                                --> 1.0.6-0                 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    curl:            7.52.1-vc14_0     defaults                                                --> 7.43.0-1                https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    hdf5:            1.8.15.1-vc14_4   defaults                                                --> 1.8.15.1-2              https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free

Proceed ([y]/n)? y

bzip2-1.0.6-0. 100% |###############################| Time: 0:00:00 630.33 kB/s
ca-certificate 100% |###############################| Time: 0:00:00   1.07 MB/s
krb5-1.13.2-0. 100% |###############################| Time: 0:00:03 424.14 kB/s
vs2015_runtime 100% |###############################| Time: 0:00:00  21.44 MB/s
vc-14.1-h0510f 100% |###############################| Time: 0:00:00   2.07 MB/s
icu-58.2-ha66f 100% |###############################| Time: 0:00:02   8.93 MB/s
jpeg-9b-hb83a4 100% |###############################| Time: 0:00:00   4.10 MB/s
openssl-1.0.2u 100% |###############################| Time: 0:00:03   1.55 MB/s
sqlite-3.30.1- 100% |###############################| Time: 0:00:00   7.00 MB/s
tk-8.6.8-hfa6e 100% |###############################| Time: 0:00:02   1.91 MB/s
xz-5.2.4-h2fa1 100% |###############################| Time: 0:00:00   8.58 MB/s
zlib-1.2.11-h6 100% |###############################| Time: 0:00:00   8.39 MB/s
curl-7.43.0-1. 100% |###############################| Time: 0:00:00   2.06 MB/s
hdf5-1.8.15.1- 100% |###############################| Time: 0:00:01 836.05 kB/s
libpng-1.6.37- 100% |###############################| Time: 0:00:00   8.05 MB/s
libssh2-1.8.0- 100% |###############################| Time: 0:00:00 557.88 kB/s
python-3.6.7-h 100% |###############################| Time: 0:00:15   1.34 MB/s
zstd-1.3.7-h50 100% |###############################| Time: 0:00:00  11.73 MB/s
freetype-2.9.1 100% |###############################| Time: 0:00:00  15.40 MB/s
libcurl-7.62.0 100% |###############################| Time: 0:00:00   1.40 MB/s
libtiff-4.1.0- 100% |###############################| Time: 0:00:00  13.28 MB/s
qt-5.6.2-vc14h 100% |###############################| Time: 0:00:59 986.70 kB/s
pillow-7.0.0-p 100% |###############################| Time: 0:00:03 193.03 kB/s
pycurl-7.43.0. 100% |###############################| Time: 0:00:00   0.00  B/s

(D:\ProgramData\Anaconda3) C:\Users\Neil\PycharmProjects\Python123>python -V
Python 3.6.7 :: Anaconda 4.3.0 (64-bit)

(D:\ProgramData\Anaconda3) C:\Users\Neil\PycharmProjects\Python123>

 

Pytorch的基本数据单元

pytorch在处理神经网络模型训练时,处理的基本数据单元是张量tensor。可以用它表示向量、矩阵。或者更高维的数据。

import torch

vector = torch.tensor([1, 2, 3, 4])
print("vector\t\t", vector)
print("vector shape\t\t", vector.shape)
print("-" * 30)

matrix = torch.tensor([[1, 2], [3, 4]])
print("matrix\t\t", matrix)
print("matrix shape\t\t", matrix.shape)
print("-" * 30)

tensor_3d = torch.tensor([[[1, 2, 1, 2], [3, 4, 3, 4], [5, 6, 5, 6]], [[1, 2, 1, 2], [3, 4, 3, 4], [5, 6, 5, 6]]])
print("tensor_3d\t\t", tensor_3d)
print("tensor_3d shape\t\t", tensor_3d.shape)
print("-" * 30)

打印结果:

amData\Anaconda3\python.exe C:/Users/Neil/PycharmProjects/Python123/Blog1/tensor_data.py
vector		 tensor([1, 2, 3, 4])
vector shape		 torch.Size([4])
------------------------------
matrix		 tensor([[1, 2],
        [3, 4]])
matrix shape		 torch.Size([2, 2])
------------------------------
tensor_3d		 tensor([[[1, 2, 1, 2],
         [3, 4, 3, 4],
         [5, 6, 5, 6]],

        [[1, 2, 1, 2],
         [3, 4, 3, 4],
         [5, 6, 5, 6]]])
tensor_3d shape		 torch.Size([2, 3, 4])
------------------------------

Process finished with exit code 0

关于神经网络部分,我下一篇再写吧。

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!