当前位置:网站首页>pytorch基础
pytorch基础
2022-06-28 12:51:00 【Gu_NN】
目录
pytorch是Facebook开发的深度学习库。详情可见 官网。
安装
为方便管理,通常在Anaconda或miniconda中新建一个虚拟环境安装pytorch。
新建虚拟环境(MacOS)
方法一:直接Anaconda Navigator中安装
在Navigator>Environments中新建环境
方法二:在终端中创建
打开终端输入如下命令:
# 查看已安装环境
conda env list
# 安装pytorch虚拟环境
conda create -n pytorch python==3.7
其他与虚拟环境操作有关命令:
# 删除指定虚拟环境
conda remove -n pytorch --all
# 激活pytorch虚拟环境
conda activate pytorch
# 退出虚拟环境
conda deactivate
安装pytorch
mac可以直接安装pytorch,官网可查安装命令。
检测安装成功与否
import torch
torch.cuda.is_available()
返回值为False则为CPU版。
基本概念
Tensor
张量(Tensor)是向量、矩阵向n维的拓展,是pytorch中进行数据存储、变换的主要工具。
创建Tensor
函数 | 举例 | 说明 |
---|---|---|
tensor(data) | x = torch.tensor([1,2]) | 生成一个1维张量[1,2] |
empty(data) | x = torch.empty(3,2) | 生成一个3行2列空张量 |
ones(sizes) | x = torch.ones(3,2) | x = [ 1 1 1 1 1 1 ] x=\begin{bmatrix}1 & 1 \\1 & 1\\1 & 1\end{bmatrix} x=⎣⎡111111⎦⎤ |
zeros(sizes) | x = torch.zeros(3,2) | x = [ 0 0 0 0 0 0 ] x=\begin{bmatrix}0 & 0 \\0 & 0 \\0 & 0\end{bmatrix} x=⎣⎡000000⎦⎤ |
eye(sizes) | x = torch.eye(3,2) | x = [ 1 0 0 1 0 0 ] x=\begin{bmatrix}1 & 0 \\0 & 1 \\0 & 0\end{bmatrix} x=⎣⎡100010⎦⎤ |
rand(sizes) | x = torch.rand(3,2) | 随机生成服从[0,1)均匀分布的3行2列张量 |
randn(sizes) | x = torch.randn(3,2) | 随机生成服从N(0,1)正态分布的3行2列张量 |
randperm(m) | x = torch.randperm(3) | 随机1维[0,6)整数随机排列 |
运算
运算 | 说明 | 函数 |
---|---|---|
加法 | x+y | z = x + y |
z = torch.add(x,y) | ||
w = torch.empty(0) torch.add(x,y,out =w) | ||
y.add_(x) | ||
减法 | x-y | z = x - y |
z = torch.sub(x,y) | ||
乘法 | 对应位置元素相乘 | z = x * y |
z = torch.mul(x,y) | ||
除法 | 对应位置元素相除 | z = x / y |
z = torch.div(x,y) | ||
矩阵叉乘 | 叉乘 | z = x @ y |
z = torch.mm(x,y) | ||
通用乘法 | x,y均为1维:点乘 x,y均为2维:差乘 x,y中有1个为1维另一个大于1位:触发广播机制 | z = torch.matmul(x,y) |
注:广播机制为当对两个形状不同的 Tensor 按元素进行运算时,先适当复制元素使这两个 Tensor 形状相同后再按元素运算。
其他操作
函数 | 举例 | 说明 |
---|---|---|
view(shape) | x.view(2,3) | 把原来3行2列的张量x改为2行3列 |
item() | x[0,0].item() | 返回x第1行1列元素数值,非Tensor格式 |
autograd
autograd是pytorch中对Tensor进行自动求导的包。当Tensor.requires_grad=True时,会自动追踪对该Tensor的所有操作。默认为False。
基本应用流程
# 生成一个追踪张量
x = torch.ones(2, 2, requires_grad=True)
y = x**2
out = y.sum()
# 反向传播
out.backward()
# 输出导数
print(x.grad)
# 新运算
out2 = x.sum()
out2.backward()
print(x.grad) #会累加out.backward()结果
# 梯度清零
out3 = x.sum()
x.grad.data.zero_()
out3.backward()
print(x.grad)
阻止跟踪
- 用 with torch.no_grad()
print(x.requires_grad)
print((x ** 2).requires_grad)
with torch.no_grad():
print((x ** 2).requires_grad)
- 用Tensor.data进行运算
x = torch.ones(1,requires_grad=True)
print(x.data) # 还是一个tensor
print(x.data.requires_grad) # return False
y = 2 * x
x.data *= 100 # 只改变了值,不会记录在计算图,所以不会影响梯度传播
y.backward()
print(x.grad)
并行计算
据说1.12版的pytorch会支持mac GPU版本。
Network partitioning
将模型分为几个部分,不同部分放到不同GPU
Layer-wise partitioning
对模型同一部分继续分为不同任务,在不同GPU同时计算
Data parallelism(主流)
拆分数据,在不同GPU上对同一模型跑不同数据
参考
边栏推荐
- Matplotlib_ Study01
- Customize MySQL connection pool
- Go language learning notes - Gorm usage - database configuration, table addition | web framework gin (VII)
- Mathematical principle derivation of structured light phase shift method + multifrequency heterodyne
- group_ Concat learning and configuration
- go template with...end遍历用法
- 腾讯确认QQ大规模盗号,iPhone14无缘Type-C,第四大运营商5G正式放号,今日更多大新闻在此...
- ##测试bug常用“Redmine”
- Unity Editor Extension Foundation, editorguilayout (III)
- 思源官方付费同步使用指南
猜你喜欢
从SimpleKV到Redis
group_concat学习与配置
命名空间和作用域
My NVIDIA developer tour -jetson nano 2GB teaches you how to train models (complete model training routines)
最新!基于Open3D的点云处理入门与实战教程
##测试bug常用“Redmine”
ASP.NET CORE Study01
如何在Microsoft Exchange 2010中安装SSL证书
centos6.5 php+mysql mysql库找不到
STM32F1与STM32CubeIDE编程实例-矩阵键盘驱动
随机推荐
Microservice stability guarantee
一款自动生成单元测试的 IDEA 插件,开发效率提升 70% 以上!
杰理之wif 干扰蓝牙【篇】
在线JSON转PlainText工具
Jerry's wif interferes with Bluetooth [chapter]
从SimpleKV到Redis
How to install SSL certificates in Microsoft Exchange 2010
Fastposter v2.8.4 release e-commerce poster generator
基于SSM实现水果蔬菜商城管理系统
I²C、SMBus、PMBus关系
Mathematical principle derivation of structured light phase shift method + multifrequency heterodyne
Après avoir échoué à l'examen d'entrée à l'Université de technologie de Harbin, vous devez rester à l'Université en tant que « chercheur » après avoir obtenu votre diplôme.
Flink流处理API大合集:掌握所有flink流处理技术,看这一篇就够了
《数字经济全景白皮书》消费金融数字化篇 重磅发布
关于IP定位查询接口的测评Ⅰ
go template with... End traversal usage
JS class 并不只是简单的语法糖!
为什么CAD导出PDF没有颜色
一文搞懂leveldb写操作
[MySQL from introduction to mastery] [advanced part] (III) creation of MySQL users_ Modification_ Delete and password settings