pytorch 介绍
是一个基于Torch的Python开源机器学习库,用于自然语言处理等应用程序。不仅能够 实现强大的GPU加速,同时还支持动态神经网络.
PyTorch提供了两个高级功能:
1.具有强大的GPU加速的张量计算(如Numpy)
2.包含自动求导系统的深度神经网络
与tensorflow,caffe区别
TensorFlow和Caffe都是命令式的编程语言,而且是静态的,首先必须构建一个神经网络,然后一次又一次使用相同的结构,如果想要改 变网络的结构,就必须从头开始。但是对于PyTorch,通过反向求导技术,可以零延迟地任意改变神经网络的行为,而且其实现速度快。
PyTorch的优点
- 支持GPU
- 灵活,支持动态神经网络
- 底层代码易于理解
- 命令式体验
- 自定义扩展
PyTorch缺点
- 目前PyTorch还不支持快速傅里 叶、沿维翻转张量和检查无穷与非数值张量;
- 针对移动端、嵌入式部署以及高性能服务器端的部署其性能表现有待提升;
- 其次因为这个框架较新,使得他的社区没有那么强大,在文档方面其C库大多数没有文档
安装
查看pytorch官网: https://pytorch.org/
查看版本:
import torch
print(torch.__version__) #注意是双下划线`
教程
参考官方文档:http://pytorch.panchuang.net/FifthSection/
关键模块
network
Dataloader
dataset
linear_layer
计算梯度
save,load
train
valid
test
基础函数
torch.max()
x = torch.randn(4,5)
# 1. max of entire tensor (torch.max(input) → Tensor)
torch.max(x)
# 2. max along a dimension (torch.max(input, dim, keepdim=False, *, out=None) → (Tensor, LongTensor))
m, idx = torch.max(x,0)
常见错误
# 1. different device error
model = torch.nn.Linear(5,1).to("cuda:0")
x = torch.Tensor([1,2,3,4,5]).to("cpu")
y = model(x)
#Tensor for 'out' is on CPU, Tensor for argument #1 'self' is on CPU, but expected them to be on GPU (while checking arguments for addmm)# 2. mismatched dimensions error
x = torch.randn(4,5)
y= torch.randn(5,4)
z = x + y
#The size of tensor a (5) must match the size of tensor b (4) at non-singleton dimension 1# 3. cuda out of memory error
import torch
import torchvision.models as models
resnet18 = models.resnet18().to("cuda:0") # Neural Networks for Image Recognition
data = torch.randn(2048,3,244,244) # Create fake data (512 images)
out = resnet18(data.to("cuda:0")) # Use Data as Input and Feed to Model
print(out.shape)
#CUDA out of memory. Tried to allocate 7.27 GiB (GPU 0; 14.76 GiB total capacity; 8.74 GiB already allocated; 4.42 GiB free; 9.42 GiB reserved in total by PyTorch)# 4. mismatched tensor type
import torch.nn as nn
L = nn.CrossEntropyLoss()
outs = torch.randn(5,5)
labels = torch.Tensor([1,2,3,4,0])
lossval = L(outs,labels) # Calculate CrossEntropyLoss between outs and labels
#expected scalar type Long but found Float
dataset and dataloader
dataset是以某种组织形式集合的数据。dataloader是可以迭代dataset的loader
在使用dataloader时,我们经常喜欢对数据进行乱序操作。这是torch.utils.data.DataLoader派上用场的地方。如果从torch.utils.data.DataLoader的角度来看每个数据都是索引(0,1,2 …),乱序可以通过打乱索引很简单做到
torch.utils.data.DataLoader需要两个信息来行使其职责。
首先,需要知道数据的长度。
其次,一旦torch.utils.data.DataLoader输出乱序结果的索引,则数据集需要返回相应的数据。
所以torch.utils.data.Dataset提供2个函数,__len__()
和 __getitem__()
import torch
import torch.utils.data
class ExampleDataset(torch.utils.data.Dataset):def __init__(self):self.data = "abcdefghijklmnopqrstuvwxyz"def __getitem__(self,idx): # if the index is idx, what will be the data?return self.data[idx]def __len__(self): # What is the length of the datasetreturn len(self.data)dataset1 = ExampleDataset() # create the dataset
dataloader = torch.utils.data.DataLoader(dataset = dataset1,shuffle = True,batch_size = 1)
for datapoint in dataloader:print(datapoint)
通过更改__len __()和__getitem __()
中的代码,可以完成一种简单的数据增强技术。
import torch
from torch.utils.data import Datasetclass TIMITDataset(Dataset):def __init__(self, X, y=None):self.data = torch.from_numpy(X).float()if y is not None:y = y.astype(np.int)self.label = torch.LongTensor(y)else:self.label = Nonedef __getitem__(self, idx):if self.label is not None:return self.data[idx], self.label[idx]else:return self.data[idx]def __len__(self):return len(self.data)
pytorch 介绍相关推荐
- [深度学习] 分布式Pytorch介绍(三)
[深度学习] 分布式模式介绍(一) [深度学习] 分布式Tensorflow介绍(二) [深度学习] 分布式Pytorch介绍(三) [深度学习] 分布式Horovod介绍(四) 一 Pytorc ...
- pytorch官方教程中文版(一)PyTorch介绍
pytorch编程环境是1.9.1+cu10.2 建议有能力的直接看官方网站英文版! 下面所示是本次教程的主要目录: pytorch官方教程中文版: PyTorch介绍 学习PyTorch 图像和视频 ...
- Pytorch Note1 Pytorch介绍
Pytorch Note1 Pytorch介绍 PyTorch的诞生 常见的深度学习框架简介 为什么选择PyTorch 总结一下Pytorch的特点 Python 优先 命令式体验 快速精益 安装 全 ...
- PyTorch 1.0 中文官方教程:PyTorch 介绍
译者:YAOKE7 Torch张量库介绍 深度学习的所有计算都是在张量上进行的,其中张量是一个可以被超过二维索引的矩阵的一般表示形式.稍后我们将详细讨论这意味着什么.首先,我们先来看一下我们可以用张量 ...
- Pytorch介绍与基本使用
前言 Pytorch是torch的python版本,是由Facebook开源的神经网络框架,专门针对 GPU 加速的深度神经网络(DNN)编程,根据我在网上的了解,相比于Tensorflow,Pyto ...
- PyTorch | (1)初识PyTorch
PyTorch | (1)初识PyTorch 介绍 PyTorch是一个非常有可能改变深度学习领域前景的Python库.我尝试使用了几星期PyTorch,然后被它的易用性所震惊,在我使用过的各种深度学 ...
- 新版PyTorch发布!新增TorchScript API,扩展ONNX导出……4大功能更新值得关注
铜灵 发自 凹非寺 量子位 出品 | 公众号 QbitAI 今天,PyTorch 1.2.0版正式发布. 官方表示,和1.1版本相比,新版本在使用体验上又往前迈进了一大步.主要新增/改动的功能包括: ...
- 撒花!PyTorch 官方教程中文版正式上线,激动人心的大好事!
点击上方"AI有道",选择"星标"公众号 重磅干货,第一时间送达 什么是 PyTorch?其实 PyTorch 可以拆成两部分:Py+Torch.Py 就是 P ...
- PyTorch机器学习从入门到实战
人工智能入门实践, 轻松玩转PyTorch框架. 校宝在线出品的书籍<PyTorch机器学习从入门到实战>发售了! 购书链接: 内容简介 近年来,基于深度学习的人工智能掀起了一股学习的热潮 ...
- 【深度学习】Github上标星1.1W的PyTorch教程,我们给您搬来了
之前给大家推过TensorFlow的入门教程,大家纷纷表示"搞研究用PyTorch它不香嘛?" 行,那说干就干.我们马上去Github上,给大家搬来了一本开源的PyTorch教程& ...
最新文章
- python自动翻译导学案_批量翻译踩过的坑--python
- 图灵71年前就已提出神经网络!《智能机器》再掀热议却一生未发表
- 黑苹果闪屏解决方法_脖子黑可以用什么方法解决,专家讲解
- PCB 内网实现《OCR文字识别》实现逻辑
- C#中获取多个对象list中对象共有的属性项
- C++去掉字符串首尾的特殊字符(比如空格)
- Hibernate ORM框架——连接池相关
- 支持上传文件的xhEditor for Typecho EX插件
- Spring事务处理之 编程式事务 和 声明式事务
- rebase冲突解决
- python string_Python String casefold()
- Tensorflow:variable变量和变量空间
- 在QTableWidget中添加QCheckBox并使其居中显示(转)
- 基于网络的 Red Hat 无人值守安装
- .net core5
- java设计模式学习-代理模式
- 使用阿里巴巴的iconfont图片库(下载到本地)
- 一周学python系列(7)——面向对象
- 家禽也能有身份,XFS在畜牧业落地
- CSS3选择器及优先级