PyTorch深度学习60分钟闪电战:01 PyTorch概览
本系列是PyTorch官网Tutorial Deep Learning with PyTorch: A 60 Minute Blitz 的翻译和总结。
- PyTorch概览
- Autograd - 自动微分
- 神经网络
- 训练一个分类器
下载本文的Jupyter NoteBook文件:60min_01_PyTorch Overview.ipynb
文章目录
- Tensor 张量
- Operations 操作
- 相加
- 索引
- Resizing
- 取值
- 与NumPy的沟通桥梁
- 将Torch Tensor转换为NumPy数组
- 将NumPy数组转换为Torch Tensor
- CUDA Tensors
Tensor 张量
Tensor类似于Numpy的ndarray,但不同的是,Tensor可以在GPU上被加速。
from __future__ import print_function
import torch
生成一个未初始化的 5×3 矩阵:
x = torch.empty(5,3)
print(x)
tensor([[0., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]])
生成一个随机矩阵:
x = torch.rand(5,3)
print(x)
tensor([[0.4907, 0.1470, 0.9752],[0.9518, 0.6973, 0.4775],[0.5643, 0.6586, 0.3142],[0.2364, 0.8435, 0.6187],[0.3253, 0.5903, 0.9939]])
生成一个0矩阵,数据类型为long
x = torch.zeros(5,3,dtype=torch.long)
print(x)
tensor([[0, 0, 0],[0, 0, 0],[0, 0, 0],[0, 0, 0],[0, 0, 0]])
直接从数据创造tensor
x = torch.tensor([6.7,6,7])
print(x)
tensor([6.7000, 6.0000, 7.0000])
在已有tensor的基础上创建tensor。这些方法将会继承原tensor的属性,比如数据类型,除非新的值由用户给出。
print('x:\n',x)
y = x.new_ones(5, 3, dtype=torch.double)
print('y:\n',y)x = torch.randn_like(x, dtype=torch.float) # dtype被改写,但是结果继承了size。
print(x)
x:tensor([6.7000, 6.0000, 7.0000])
y:tensor([[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.]], dtype=torch.float64)
tensor([-0.3186, 0.5506, 0.4940])
获得tensor的size
print(x.size())
torch.Size([3])
Operations 操作
相加
PyTorch中有许多种相加语法:
**相加:**语法1
x = torch.rand(5, 3)
y = torch.rand(5, 3)
print(x + y)
tensor([[0.9943, 0.3976, 0.6645],[0.6796, 1.1703, 1.2389],[1.5879, 1.1208, 1.5912],[1.2165, 0.8195, 1.0364],[1.2178, 1.3195, 1.1736]])
**相加:**语法2
print(torch.add(x,y))
tensor([[0.9943, 0.3976, 0.6645],[0.6796, 1.1703, 1.2389],[1.5879, 1.1208, 1.5912],[1.2165, 0.8195, 1.0364],[1.2178, 1.3195, 1.1736]])
**相加:**将一个tensor作为out参数,用以输出。
result = torch.empty(5,3)
torch.add(x,y,out=result)
print(result)
tensor([[0.9943, 0.3976, 0.6645],[0.6796, 1.1703, 1.2389],[1.5879, 1.1208, 1.5912],[1.2165, 0.8195, 1.0364],[1.2178, 1.3195, 1.1736]])
**相加:**替代
y.add_(x)
print(y)
tensor([[0.9943, 0.3976, 0.6645],[0.6796, 1.1703, 1.2389],[1.5879, 1.1208, 1.5912],[1.2165, 0.8195, 1.0364],[1.2178, 1.3195, 1.1736]])
Note:
任何让tensor被覆盖的方法的名称都以
_
结尾。例如x.copy_(y)
,x.t_()
等。
索引
NumPy中的索引方法可以使用:
print(x[:, 1])
tensor([0.0200, 0.4981, 0.2466, 0.7497, 0.8315])
Resizing
想要改变tensor的规模,可以使用torch.view
。
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8) # the size -1 is inferred from other dimensions
print(x.size(), y.size(), z.size())
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
取值
如果你有一个单元素的tensor,.item()
方法可以取值。
x = torch.randn(1)
print(x)
print(x.item())
tensor([-0.7093])
-0.709314227104187
与NumPy的沟通桥梁
将Torch张量转换为NumPy数组很容易,反之亦然。
Torch张量将会和NumPy数组共享同一个内存位置(Torch张量在CPU上的情况下),其中一个发生改变,另一个也会随之改变。
将Torch Tensor转换为NumPy数组
a = torch.ones(5)
print(a)
b = a.numpy()
print(b)
a.add_(1)
print(a)
print(b)
tensor([1., 1., 1., 1., 1.])
[1. 1. 1. 1. 1.]
tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]
将NumPy数组转换为Torch Tensor
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
除了CharTensor,其他在CPU上的Torch张量均支持与NumPy数组的相互转换。
CUDA Tensors
使用.to()
方法,可以将张量转移到其他设备。
if torch.cuda.is_available():device = torch.device("cuda") # 创建CUDA设备对象y = torch.ones_like(x, device=device) # 直接在GPU上x = x.to(device)# x = x.to("cuda") 也可以这么写z = x + yprint(z)print(z.to("cpu", torch.double)) # ``.to`` 方法可以同时转换数据类型
tensor([0.2907], device='cuda:0')
tensor([0.2907], dtype=torch.float64)
PyTorch深度学习60分钟闪电战:01 PyTorch概览相关推荐
- PyTorch深度学习60分钟闪电战:03 神经网络
本系列是PyTorch官网Tutorial Deep Learning with PyTorch: A 60 Minute Blitz 的翻译和总结. PyTorch概览 Autograd - 自动微 ...
- PyTorch深度学习60分钟闪电战:04 训练一个分类器
本系列是PyTorch官网Tutorial Deep Learning with PyTorch: A 60 Minute Blitz 的翻译和总结. PyTorch概览 Autograd - 自动微 ...
- PyTorch深度学习60分钟闪电战:02 Autograd - 自动微分
本系列是PyTorch官网Tutorial Deep Learning with PyTorch: A 60 Minute Blitz 的翻译和总结. PyTorch概览 Autograd - 自动微 ...
- PyTorch 深度学习: 60 分钟极速入门
PyTorch 深度学习: 60 分钟极速入门 2019年年初,ApacheCN组织志愿者翻译了PyTorch1.2版本中文文档(github地址),同时也获得了PyTorch官方授权,我相信已经有许 ...
- PyTorch深度学习60分钟入门与实战(四)训练分类器
原文:github link,最新版会首先更新在github上 有误的地方拜托大家指出~ 训练分类器 目前为止,我们以及看到了如何定义网络,计算损失,并更新网络的权重. 现在可能会想, 数据呢? 通常 ...
- pytorch | 深度学习分割网络U-net的pytorch模型实现
原文:https://blog.csdn.net/u014722627/article/details/60883185 pytorch | 深度学习分割网络U-net的pytorch模型实现 这个是 ...
- pytorch深度学习_了解如何使用PyTorch进行深度学习
pytorch深度学习 PyTorch is an open source machine learning library for Python that facilitates building ...
- PyTorch 深度学习:34分钟快速入门——自动编码器
自动编码器最开始是作为一种数据压缩方法,同时还可以在卷积网络中进行逐层预训练,但是随后更多结构复杂的网络,比如 resnet 的出现使得我们能够训练任意深度的网络,自动编码器就不再使用在这个方面,下面 ...
- PyTorch 深度学习:32分钟快速入门——ResNet
ResNet 当大家还在惊叹 GoogLeNet 的 inception 结构的时候,微软亚洲研究院的研究员已经在设计更深但结构更加简单的网络 ResNet,并且凭借这个网络子在 2015 年 Ima ...
最新文章
- 【物联网】NB-IoT简介
- 裸centos安装PCRE时报错解决
- 桌面的html文件怎么打开方式,html文件怎么打开
- GS01创建信息集及其他GROUP在ABAP程序中的调用方法
- JAVA笔记11__File类/File类作业/字节输出流、输入流/字符输出流、输入流/文件复制/转换流...
- python安装后无法运行任何软件_为啥我按照python安装教程,总说无法启动此程序,因为计算机中丢失?...
- java面试题(96~125)《中》
- 优酷的多页画在同一窗口打开效果。
- PHP实现伪静态化页面的具体实现方式
- font-family常见中文字体对应的英文名称
- PLC编程技术的发展及应用
- c语言获取Windows缓存,【图片】【C语言】【Windows】--IE缓存提取器【erbi_lucifer吧】_百度贴吧...
- PyCharm常用配置和常用插件
- js 获得较浅的颜色_了解较少的颜色功能
- 自定义结构体及初始化
- ArrayList和Linked的区别
- 最新版Nessus的安装
- web文件上传-0x00漏洞
- Firecracker
- 基于用户点击偏好和阅读满意度的个性化新闻推荐技术