pytorch 全局变量

by Déborah Mesquita

由DéborahMesquita

Pytorch如何通过深度学习展现全局 (How Pytorch gives the big picture with deep learning)

Some time ago we saw how to classify texts with neural networks. The article covered the following topics:

前段时间,我们看到了如何使用神经网络对文本进行分类 。 本文介绍了以下主题:

  • What is a machine learning model什么是机器学习模型
  • What is a neural network什么是神经网络
  • How the neural network learns神经网络如何学习
  • How to manipulate data and pass it to the neural network inputs如何处理数据并将其传递给神经网络输入
  • How to run the model and get the results for the prediction如何运行模型并获得预测结果

In today’s article, we are going to build the same network, but instead of using TensorFlow, we are going to use Pytorch. We’ll focus only on the code. So if you need a primer on neural networks, it’s a good idea to check out the previous article. :)

在今天的文章中,我们将构建相同的网络,但是我们将使用Pytorch而不是使用TensorFlow 我们将只关注代码。 因此,如果您需要有关神经网络的入门知识,那么最好查阅上一篇文章 。 :)

We’ll create a machine learning model that classifies texts into categories. The dataset is the 20 Newsgroups, which contains 18,000 posts about 20 different topics. We will use only 3 categories: comp.graphics, sci.space, and rec.sport.baseball.

我们将创建一个将文本分类的机器学习模型。 数据集是20个新闻组 ,其中包含有关20个不同主题的18,000个帖子。 我们将仅使用3个类别:comp.graphics,sci.space和rec.sport.baseball。

什么是Pytorch? (What is Pytorch?)

Pytorch is a Python-based scientific computing package that is a replacement for NumPy, and uses the power of Graphics Processing Units. It is also a deep learning research platform that provides maximum flexibility and speed.

Pytorch是基于Python的科学计算程序包,它替代了NumPy并使用了图形处理单元的功能。 它也是提供最大灵活性和速度的深度学习研究平台。

The biggest difference between Pytorch and Tensorflow is that Pytorch can create graphs on the fly. This makes debugging so much easier (and fun!).

Pytorch和Tensorflow之间的最大区别是Pytorch可以动态创建图形。 这使得调试非常容易(而且很有趣!)。

When you execute a line of code, it gets executed. There isn’t an asynchronous view of the world. When you drop it into a debugger, or receive error messages and stack traces, understanding them is straight forward. The stack trace points to exactly where your code was defined.

当您执行一行代码时,它就会被执行。 没有对世界的异步看法。 当您将其放入调试器中或收到错误消息和堆栈跟踪时,直接理解它们即可。 堆栈跟踪指向您定义代码的确切位置。

建立网络 (Building the network)

Ok, let’s see how things work in Pytorch.

好的,让我们看看Pytorch的工作原理。

基础 (The basics)

As usual, we have tensors, which are multi-dimensional matrices that contain elements of a single data type.

像往常一样,我们有张量 ,它们是包含单个数据类型元素的多维矩阵。

The torch package contains data structures for multi-dimensional tensors and mathematical operations.

火炬软件包包含用于多维张量和数学运算的数据结构。

  • torch.nn is a neural network library deeply integrated with autograd, and is designed for maximum flexibility

    torch.nn是与autograd深度集成的神经网络库,旨在最大程度地提高灵活性

  • torch.autograd is a tape-based automatic differentiation library that supports all differentiable Tensor operations in torch

    torch.autograd是基于磁带的自动差异库,它支持炬管中所有可微分的Tensor操作

步骤1:定义网络 (Step 1: Define the network)

With TensorFlow each layer operation has to be explicitly named:

使用TensorFlow时,每个层操作必须明确命名:

With Pytorch we use torch.nn. We need to multiply each input node with a weight, and also to add a bias. The classtorch.nn.Linear does the job for us.

通过Pytorch,我们使用torch.nn 。 我们需要将每个输入节点与权重相乘,并且还要添加一个偏差。 torch.nn.Linear类为我们完成了工作。

  • torch.nn.Linear applies a linear transformation to the incoming data, y=Ax+b

    torch.nn.Linear对输入数据进行线性变换, y = Ax + b

The base class for all neural network modules is torch.nn.Module. The forward(*input) defines the computation performed at every call, and all subclasses should override it.

所有神经网络模块的基类是torch.nn.Moduleforward (* input)定义了每次调用时执行的计算,所有子类都应覆盖它。

Cool, right?

酷吧?

步骤2:更新权重 (Step 2: Update the weights)

The way the neural network “learns” is by updating the weight values. With Pytorch we use the torch.autograd package to do that.

神经网络“学习”的方式是通过更新权重值。 使用Pytorch,我们使用torch.autograd包来执行此操作。

Torch.autograd.Variable wraps a tensor and records the operations applied to it. This is very handy and allows us to work with the gradient descent in a very simple way. Let’s have a closer look at the documentation.

Torch.autograd.Variable包装张量并记录对其应用的操作。 这非常方便,使我们能够以非常简单的方式处理梯度下降。 让我们仔细看一下文档。

A variable is a thin wrapper around a Tensor object that also holds the gradient and a reference to the function that created it. This reference allows us to trace the entire chain of operations that created the data.

变量是围绕Tensor对象的薄包装,它还保存渐变和对创建渐变的函数的引用。 此参考使我们能够跟踪创建数据的整个操作链。

We didn’t specify the weight tensors like we did with TensorFlow because the torch.nn.Linear class has a variable weight with shape (out_features x in_features).

我们没有像TensorFlow那样指定权重张量,因为torch.nn.Linear类具有可变的权重形状(out_features x in_features)。

  • torch.nn.Linear(in_features, out_features, bias=True)

    torch.nn.Linear (in_features,out_features,bias = True)

To compute the gradient, we will use the the method Adaptive Moment Estimation (Adam). Torch.optim is a package that implements various optimization algorithms.

为了计算梯度,我们将使用自适应矩估计(Adam)方法 。 Torch.optim是一个实现各种优化算法的软件包。

To use torch.optim, you have to construct an optimizer object that will hold the current state and also update the parameters based on the computed gradients.

要使用torch.optim ,您必须构造一个优化器对象,该对象将保留当前状态,并根据计算出的梯度来更新参数。

To construct an optimizer, you have to give it an iterable that contains the parameters (all should be variables ) to optimize. Then you can specify options that are specific to an optimizer, such as the learning rate, weight decay, etc.

要构建optimizer ,您必须给它提供一个可迭代的variable s ,其中包含要优化的参数(所有参数都应为variable s )。 然后,您可以指定特定于优化程序的选项,例如学习率,权重衰减等。

Let’s construct our optimizer:

让我们构造我们的优化器:

The parameters() method from torch.nn.Module returns an iterator over the module parameters. To compute the loss we’ll use torch.nn.CrossEntropyLoss

torch.nn.Module中parameters()方法返回模块参数上的迭代器。 为了计算损失,我们将使用torch.nn.CrossEntropyLoss

One important thing about torch.nn.CrossEntropyLoss is that input has to be a 2D tensor of size (minibatch, n) and target expects a class index (0 to nClasses-1) as the target for each value of a 1D tensor of size minibatch. For example:

关于torch.nn.CrossEntropyLoss重要一件事是输入必须是大小为2D的张量 (小批量,n) 目标期望将类别索引(0到nClasses-1)作为大小为minibatch的一维张量的每个值的目标。 例如:

So we need to change the get_batch() function from the previous article to work like it does in the example above.

因此,我们需要更改上一篇文章中的get_batch()函数,使其像上面的示例一样工作。

Now let’s update the weights and see the magic of the variables.

现在,让我们更新权重,看看变量的妙处。

The method torch.autograd.backward computes the sum of the gradients for given variables. As the documentation says, this function accumulates gradients in the leaves, so you might need to zero them before calling them. To update the parameters, all optimizers implement a step() method. The functions can be called once the gradients are computed, for example you can use backward() to call them.

torch.autograd.backward方法计算给定变量的梯度总和。 如文档所述,此函数会在树叶中累积渐变, 因此您可能需要先将它们调零,然后再调用它们 。 为了更新参数,所有优化器都实现了step()方法。 一旦计算出梯度,就可以调用这些函数,例如,您可以使用backward( ) 给他们打电话。

In the neural network terminology, one epoch equals one forward pass (getting the output values), and one backward pass (updating the weights) equals all the training examples. In our network, the get_batch() function gives us the number of texts with the size of the batch.

在神经网络术语中 ,一个历元等于一个正向通过(获取输出值),而一个反向等于(更新权重)等于所有训练示例。 在我们的网络中, get_batch()函数为我们提供了具有批处理大小的文本数。

Putting it all together, we get this:

将所有内容放在一起,我们得到以下信息:

And that’s it.

就是这样。

I never thought I would say this about a piece of code, but it’s beautiful.

我从没想过我会讲这段代码,但这很漂亮。

Isn’t it?

是不是

Now let’s test the model:

现在让我们测试模型:

And that’s it.

就是这样。

You have created a model using a neural network to classify texts into categories.

您已经使用神经网络创建了一个模型来将文本分类。

Congratulations. ?

恭喜你 ?

You can see the notebook with the final code here.

您可以在此处查看带有最终代码的笔记本。

Did you found this article helpful? I try my best to write a deep dive article each month, you can receive an email when I publish a new one.

您觉得这篇文章对您有帮助吗? 我每个月都会尽力写一篇深入的文章, 当我发布新文章时,您会收到一封电子邮件 。

翻译自: https://www.freecodecamp.org/news/how-pytoch-gives-the-big-picture-with-deep-learning-e4a0f372f4b6/

pytorch 全局变量

pytorch 全局变量_Pytorch如何通过深度学习展现全局相关推荐

  1. 364 页 PyTorch 版《动手学深度学习》PDF 开源了(全中文,支持 Jupyter 运行)

    点击上方"AI有道",选择"星标"公众号 重磅干货,第一时间送达 李沐,亚马逊 AI 主任科学家,名声在外!半年前,由李沐.Aston Zhang 等人合力打造 ...

  2. 364 页 PyTorch 版《动手学深度学习》分享(全中文,支持 Jupyter 运行)

    1 前言 最近有朋友留言要求分享一下李沐老师的<动手学深度学习>,小汤本着一直坚持的"好资源大家一起分享,共同学习,共同进步"的初衷,于是便去找了资料,而且还是中文版的 ...

  3. 神经网络学习小记录69——Pytorch 使用Google Colab进行深度学习

    神经网络学习小记录69--Pytorch 使用Google Colab进行深度学习 注意事项 学习前言 什么是Google Colab 相关链接 利用Colab进行训练 一.数据集与预训练权重的上传 ...

  4. 【深度学习】——利用pytorch搭建一个完整的深度学习项目(构建模型、加载数据集、参数配置、训练、模型保存、预测)

    目录 一.深度学习项目的基本构成 二.实战(猫狗分类) 1.数据集下载 2.dataset.py文件 3.model.py 4.config.py 5.predict.py 一.深度学习项目的基本构成 ...

  5. pytorch 模型可视化_【深度学习】高效使用Pytorch的6个技巧:为你的训练Pipeline提供强大动力...

    作者:Eugene Khvedchenya   编译:ronghuaiyang 导读 只报告模型的Top-1准确率往往是不够的. 将train.py脚本转换为具有一些附加特性的强大pipeline 每 ...

  6. pytorch卷积神经网络_【深度学习】卷积神经网络图片分类案例(pytorch实现)

    文 | 菊子皮 (转载请注明出处)B站:科皮子菊 前言 前文已经介绍过卷积神经网络的基本概念[深度学习]卷积神经网络-CNN简单理论介绍[1].下面开始动手实践吧.本文任务描述如下:从公开数据集CIF ...

  7. pytorch 预测手写体数字_深度学习之PyTorch实战(3)——实战手写数字识别

    如果需要小编其他论文翻译,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/DeepLearningNote 上一节,我们已经 ...

  8. 同时安装pytorch和TensorFlow等多种深度学习开发环境(1)

    现在的开发环境比较多,在学习的过程中,经常会使用到不同的开发环境,最常用的就是TensorFlow和pytorch,以及其他的开发环境,我在学习的过程中使用的开发环境主要有pytorch和Tensor ...

  9. 中文教程!PyTorch版《动手学深度学习》开源了,最美DL书遇上最赞DL框架

    点击我爱计算机视觉标星,更快获取CVML新技术 本文经机器之心(微信公众号:almosthuman2014)授权转载,禁止二次转载 机器之心报道 项目作者:ShusenTang 参与:思 想要入门最前 ...

最新文章

  1. 困惑度 (perplexity)
  2. Python第五课(字典)
  3. 《Adobe Photoshop大师班:经典作品与完美技巧赏析》—Alexander Corvus
  4. GGNN(Gated Graph Sequence Neural Networks)
  5. spyder中绘图无法显示负号_matlibplot+seaborn绘图风格交叉使用
  6. 蓝桥杯练习系统习题-历年真题解析2(完整版)
  7. 如何优化 Linux系统
  8. 牛客题霸 [有关阶乘的两个问题1] C++题解/答案
  9. 计算机四级的英文,计算机四级考试中英文术语对照
  10. Java PriorityQueue poll()方法与示例
  11. Android studio3.5 SDK29项目开发笔记
  12. eclipse护眼颜色和字体大小设置
  13. angularjs中使用ng-repeat渲染最后一个li的时候设置不同样式
  14. CrystalBall
  15. 小爱音箱mini系统故障怎么办_Win7系统连接小爱音箱mini的方法【图文】
  16. 矩阵的转置与求导运算
  17. OKHttp原理讲解之责任链模式及扩展
  18. 【量化笔记】时间序列--ARCH模型及GARCH模型
  19. 求方程ax^2+bx+c=0的根,用3个函数分别求当b^2-4ac大于0、等于0和小于0时的根,并输出结果。 从主函数输入a, b, c的值。
  20. 购买代购的产品算违法吗——看空姐代购被判刑有感

热门文章

  1. 【Linux入门学习之】vi/vim编辑器必知必会
  2. 关于windows窗体应用程序 1117
  3. 学生对象数组实现按年龄排序 按成绩排序1022
  4. jquery-选择器-筛选器
  5. nginx配置静态资源允许跨域访问
  6. laravel-admin form中的数据,在提交后,保存前,获取并进行编辑
  7. iOS高仿微信项目、阴影圆角渐变色效果、卡片动画、波浪动画、路由框架等源码...
  8. 这些深度学习术语,你了解多少?(上)
  9. java实现权重随机算法
  10. 用C#实现pdf文件的完整性验证