2020/01/31 16:17

What  is Pytorch

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 定义梯度：应用在反向传播中

# 第二步：计算
# 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对有数据的占位符进行运算



pip install protobuf==3.6.1

（本文出自oschina博主happyBKs的博文：https://my.oschina.net/happyBKs/blog/3162262，转载请注明出处）

pytorch代码实现

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

import torch

N, D = 3, 4

# 定义变量，定义的同时可以赋值初始化

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

c.backward()


安装pytorch

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

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
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）如果不能解决，记得试一试）

(D:\ProgramData\Anaconda3) C:\Users\Neil\PycharmProjects\Python123>conda install python=3.6.7
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

0
0 收藏

0 评论
0 收藏
0