1、环境配置
Windows10
Pytorch1.4
VS2015
Python3.6.5
Anaconda3-5.3.1-Windows-x86_64
2、Anaconda换源
2.1Anaconda
Anaconda指的是一个开源的Python发行版本,其包含了conda、Python等180多个科学包及其依赖项。当你尝试pip install xxx时出现各种意外和依赖问题,那么conda就是一方良药。可以让你轻松的安装各种库并处理各种依赖问题。
2.2Anaconda安装
可以从官网下载,不过服务器在国外,所以很慢。推荐使用国内镜像网站:清华大学开源镜像站:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/,下载后一直next下去安装完成即可,安装位置可以自定义。可以自己勾选add to path添加系统环境变量,或者自己安装完成后手动添加。(没有添加就不能在控制台进行命令操作)
2.3 换源
Anaconda换源
由于服务器在国外,所以更新下载很慢,建议换源到国内镜像源,在cmd控制台或者powershell下输入命令即可
清华anaconda镜像:
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额外库:
# pytorch
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
# 安装时PyTorch,官网给的安装命令需要去掉最后的-c pytorch,才能使用清华源
# conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
# msys2
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
# bioconda
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
# menpo
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
# 设置搜索时显示通道地址
conda config --set show_channel_urls yes
额外库都是第三方提供的,非anaconda官方的,建议没有特殊需要直接使用稳定的官方库。
中科大anaconda镜像:
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
conda config --set show_channel_urls yes
国内源也是会挂的,之前清华就挂过,后来又活了,腾讯挂了就直接死了,一旦出现一直连接失败问题就可以换回来了。
换回默认源:conda config --remove-key channels
3、基本语法演示
from __future__ import print_function
import torch
import numpy as np
print(torch.__version__)
# 定义矩阵
x = torch.empty(2, 2)
print(x)
# 定义随机初始化矩阵
x = torch.randn(2, 2)
print(x)
# 定义初始化为零
x = torch.zeros(3, 3)
print(x)
# 定义数据为tensor
x = torch.tensor([5.1, 2., 3., 1.])
print(x)
# 操作
a = torch.tensor([1.,2.,3.,4.,5.,6.,7.,8.])
b = torch.tensor([11.,12.,13.,14.,15.,16.,17.,18.])
c = a.add(b)
print(c)
# 维度变换 2x4
a = a.view(-1, 4)
b = b.view(-1, 4)
c = torch.add(a, b)
print(c, a.size(), b.size())
# torch to numpy and visa
na = a.numpy()
nb = b.numpy()
print("\na =",na,"\nb =", nb)
# 操作
d = np.array([21.,22.,23.,24.,25.,26.,27.,28.], dtype=np.float32)
print(d.reshape(2, 4))
d = torch.from_numpy(d.reshape(2, 4))
sum = torch.sub(c, d)
print(sum, "\n sum = ", sum.size())
# using CUDA
if torch.cuda.is_available():
result = d.cuda() + c.cuda()
print("\n result = ", result)
# 自动梯度
x = torch.randn(1, 5, requires_grad=True)
y = torch.randn(5, 3, requires_grad=True)
z = torch.randn(3, 1, requires_grad=True)
print("\nx=",x, "\ny=",y, "\nz=",z)
xy = torch.matmul(x, y)
xyz = torch.matmul(xy, z)
xyz.backward()
print(x.grad, y.grad, z.grad)
4、运行输出结果:
1.4.0
tensor([[1.1210e-44, 0.0000e+00],
[1.4013e-45, 0.0000e+00]])
tensor([[-0.2395, -0.0214],
[-0.1803, -0.1696]])
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
tensor([5.1000, 2.0000, 3.0000, 1.0000])
tensor([12., 14., 16., 18., 20., 22., 24., 26.])
tensor([[12., 14., 16., 18.],
[20., 22., 24., 26.]]) torch.Size([2, 4]) torch.Size([2, 4])
a = [[1. 2. 3. 4.]
[5. 6. 7. 8.]]
b = [[11. 12. 13. 14.]
[15. 16. 17. 18.]]
[[21. 22. 23. 24.]
[25. 26. 27. 28.]]
tensor([[-9., -8., -7., -6.],
[-5., -4., -3., -2.]])
sum = torch.Size([2, 4])
x= tensor([[-0.2528, -0.7547, -0.3037, 1.1205, -0.8601]], requires_grad=True)
y= tensor([[-0.2925, -0.5226, -0.5754],
[ 0.2068, -0.7954, 0.2380],
[ 1.4205, -1.4876, 0.7595],
[ 1.0451, 2.0408, 0.3937],
[-0.6681, -0.3139, -0.1313]], requires_grad=True)
z= tensor([[-2.5003],
[ 0.6722],
[-1.9335]], requires_grad=True)
tensor([[ 1.4927, -1.5117, -6.0203, -2.0024, 1.7133]]) tensor([[ 0.6320, -0.1699, 0.4887],
[ 1.8869, -0.5073, 1.4592],
[ 0.7594, -0.2042, 0.5873],
[-2.8015, 0.7531, -2.1664],
[ 2.1506, -0.5781, 1.6630]]) tensor([[1.2320],
[3.7408],
[0.2892]])
这块为学习篇,学习资源来源于opencv学堂贾老师的知识星球。
希望跟大家一起学习,加油吧 阿超没有蛀牙!