keras pytorch

介绍 (Introduction)

The book “Deep Learning with Python” by Francois Chollet (creator of Keras) was the thing that got me into the world of deep learning. Since then I had fallen in love with the style of Keras.

Francois Chollet(Keras的创建者)写的《用Python进行深度学习》一书使我进入了深度学习领域。 从那时起,我就爱上了Keras的风格。

Keras was my first framework, then jumped into a little bit of Tensorflow and then came in PyTorch, the rest is history.

Keras是我的第一个框架,然后跳入了一点Tensorflow,然后进入PyTorch,剩下的就是历史了。

To be honest, I was really excited by the progress bar that shows up during the model training in Keras, it’s just awesome :)

老实说,在Keras进行模型训练时出现的进度栏让我感到非常兴奋,这真是太棒了:)

So, why not try to bring the Keras experience of training models to PyTorch?

那么,为什么不尝试将Keras训练模型的经验引入PyTorch?

This question got me started in and I ended up recreating the dense layer, convolutional layer and flatten layer of Keras with all that fancy progress bars.

这个问题使我开始,最后我用所有花哨的进度条重新创建了Keras的密集层,卷积层和平坦层。

Models can be created by stacking one layer on top of the other and trained by simply calling the fit method which is similar to how Keras does the job.

可以通过将一层堆叠在另一层之上来创建模型,并可以通过简单地调用与Keras的工作方式类似的fit方法来对其进行训练。

让我们来建立它 (Let’s build it)

For those of you who haven’t worked with Keras, building and training a model in Keras looks something like below:

对于尚未与Keras合作的人,在Keras中构建和训练模型如下所示:

Training a fully-connected network in keras
在keras中训练完全连接的网络

1.Import the required libraries

1.导入所需的库

You may not be familiar with the library pkbar, it is used for displaying the Keras like progress bar.

您可能不熟悉库pkbar,它用于显示类似进度条的Keras。

Importing required libraries
导入所需的库

2. Input layer and dense layer

2.输入层和密集层

The input layer simply takes in the shape of a single instance of the data that will be passed to the neural network and return it, for fully-connected networks it will be something like (1, 784) and for convolutional neural network, it will be the dimensions of the image(height×width×channels).

输入层只是采用将传递到神经网络并返回数据的单个实例的形状,对于完全连接的网络,它将类似于(1,784),对于卷积神经网络,它将是图像的尺寸(高度×宽度×通道)。

Using capital letters for naming python function is against the rules, but we will neglect it for the time being(some parts of Keras source code uses the same convention).

使用大写字母命名python函数是违反规则的,但我们暂时将其忽略(Keras源代码的某些部分使用相同的约定)。

Input layer
输入层

The dense class is initialized by passing the number of output neurons and activation function for that layer. When the dense layer is called, the previous layers is passed as the input.

通过传递输出神经元的数量和该层的激活函数来初始化密集类。 调用密集层时,先前的层将作为输入传递。

Now we have the information about the previous layer. If the previous layer is input layer, a PyTorch linear layer is created with shape returned from the input layer and the number of output neurons provided as an argument during dense class initialization.

现在我们有了有关上一层的信息。 如果上一层是输入层,则会创建一个PyTorch线性层,其形状从输入层返回,并在密集类初始化期间提供作为参数的输出神经元数。

If the previous layer is a dense layer, we extend the neural network by adding a PyTorch linear layer and an activation layer provided to the dense class by the user.

如果前一层是密集层,我们通过添加PyTorch线性层和用户提供给密集类的激活层来扩展神经网络。

And if the previous layer is a convolution or flatten layer, we will create a utility function called get_conv_output() to get the output shape of the image after passing through the convolution and flatten layers. This dimension is required because we cannot create linear layer in PyTorch without passing a value to the in_features argument.

如果前一层是卷积层或展平层,我们将创建一个名为get_conv_output()的实用程序函数,以在经过卷积层和展平层后获得图像的输出形状。 此尺寸是必需的,因为如果不将值传递给in_features参数,则无法在PyTorch中创建线性图层。

The get_conv_output() function takes in the image shape and the convolution neural network model as input. It then creates a dummy tensor with the same shape as the image and passes it to the convolutional network(with flatten layer) and returns the size of the data coming out of it, this size is passed as value to the in_features argument in PyTorch’s linear layer.

get_conv_output()函数采用图像形状和卷积神经网络模型作为输入。 然后创建一个与图像形状相同的虚拟张量,然后将其传递到卷积网络(具有平坦层),并返回从其中得到的数据的大小,该大小作为值传递给PyTorch线性模型中的in_features参数层。

Dense layer
致密层

3. Flatten layer

3.展平层

For the purpose of creating a flatten layer, we will be creating a custom layer class called flattened layer which takes in a tensor as input and returns the flattened version of the tensor during the forward propagation.

为了创建扁平化层,我们将创建一个称为flattened layer的自定义层类,该类将一个张量作为输入并在正向传播期间返回该张量的扁平化版本。

We will create another class called flatten, when this layer is called, the previous layers is passed as input, then the flatten class extends the network by adding our custom created flattened layer class on top of the previous layers.

我们将创建另一个名为flatten的类,当调用此层时,会将先前的层作为输入传递,然后通过在先前层的顶部添加我们自定义创建的flattened层类来扩展该网络。

Thus all the data coming to the flatten layer is flattened using our custom created flattened layer class.

因此,使用我们自定义创建的展平层类将展平层中的所有数据展平。

Flatten layer
展平层

4. Convolutional layer

4.卷积层

We will initialize the Conv2d layer by passing in the number of filters, kernel size, strides, padding, dilation and activation function.

我们将通过传入过滤器的数量,内核大小,步幅,填充,扩张和激活函数来初始化Conv2d层。

Now, when the Conv2d layer is called, the previous layers is passed to it, if the previous layer is Input layer, a single PyTorch Conv2d layer with the provided values of number of filters, kernel size, strides, padding, dilation and activation function is created where the value of in_channels is taken from the number of channels in the input shape.

现在,当Conv2d层被调用时,先前的层将传递给它,如果先前的层是Input层,则是一个PyTorch Conv2d层,其提供的值包括过滤器数量,内核大小,步幅,填充,扩张和激活函数在从输入形状中的通道数获取in_channels值的位置创建。

If the previous layer is a convolutional layer, previous layer is extended by adding a PyTorch Conv2d layer and activation function with the value of in_channels taken from the out_channels of previous layer.

如果前一层是卷积层,则通过添加PyTorch Conv2d层和激活函数来扩展前一层,该函数具有从前一层的out_channels中获取的in_channels值。

In the case of padding, if the user needs to preserve the dimensions of data going out of that layer, then the value of padding can be specified as ‘same’ instead of an integer.

在填充的情况下,如果用户需要保留从该层出来的数据的尺寸,则可以将填充的值指定为“相同”而不是整数。

If the value of padding is specified as ‘same’, then a utility function called same_pad() is used to get the value of padding to preserve the dimensions for a given input size, kernel size, stride and dilation.

如果将padding的值指定为“ same”,则将使用一个名为same_pad()的实用函数来获取padding的值,以保留给定输入大小,内核大小,步幅和膨胀的尺寸。

The input size can be obtained using the get_conv_output() utility function discussed earlier.

可以使用前面讨论的get_conv_output()实用程序函数获得输入大小。

5. Model class

5.模型类

After building the architecture of our model, the Model class is initialized by passing in the input layer and the output layer. But I have given an extra argument called device which is not present in Keras, this argument takes in the value as either ‘CPU’ or ‘CUDA’ which will move the entire model to the specified device.

构建完模型的体系结构后,通过传入输入层和输出层来初始化Model类。 但是我给了一个额外的参数,称为设备,它在Keras中不存在,该参数接受的值是'CPU'或'CUDA',它将把整个模型移动到指定的设备。

The model class’s parameters method is used to return the parameters of the model which is to be given to PyTorch optimizer.

模型类的参数方法用于返回要提供给PyTorch优化器的模型参数。

The model class has a method called compile which takes in the optimizer and loss function needed for training the model. The summary method of model class displays the summary of created model with the help of torchsummary library.

模型类具有一种称为compile的方法,该方法采用了优化器和训练模型所需的损失函数。 模型类的summary方法借助torchsummary库显示所创建模型的摘要。

The fit method is used for training the model, this method takes the input feature set, target data set and the number of epochs as argument. It displays the loss calculated by the loss function and progress of the training using the pkbar library.

fit方法用于训练模型,该方法以输入特征集,目标数据集和历元数为参数。 它显示由损失函数计算的损失以及使用pkbar库的训练进度。

The evaluate method is used to calculate the loss and accuracy on the test data.

评估方法用于计算测试数据的损失和准确性。

The fit_generator, evaluate_generator and predict_generator is used when the data is loaded using PyTorch data loader. The fit_generator takes the training set data loader and epochs as arguments. The evaluate_generator and predict_generator takes the validation set data loader and test data loader respectively to measure how well the model is performing on unseen data.

当使用PyTorch数据加载器加载数据时,将使用fit_generator,valuate_generator和predict_generator。 fit_generator将训练集数据加载器和纪元作为参数。 valuate_generator和predict_generator分别使用验证集数据加载器和测试数据加载器来衡量模型在看不见的数据上的性能。

Model class
模型类

最后的想法 (Final thoughts)

I’ve tested the code on CIFAR100, CIFAR10 and MNIST data set using both dense layer and convolutional neural networks. It works fine, but there is a huge space for improvement.

我已经使用密集层和卷积神经网络在CIFAR100,CIFAR10和MNIST数据集上测试了代码。 它工作正常,但仍有很大的改进空间。

This was a fun project that I was working for 3–4 days and it really pushed my limits of programming with PyTorch.

这是一个有趣的项目,我花了3-4天的时间,这确实突破了我使用PyTorch编程的极限。

You can take a look at the complete code with training done on the above-mentioned data sets here or you can freely tweak the code to suit your liking in colab.

您可以在完整的代码看看与上述数据集训练做在这里也可以自由调整,以满足您在喜欢的代码colab 。

翻译自: https://towardsdatascience.com/recreating-keras-functional-api-with-pytorch-cc2974f7143c

keras pytorch


http://www.taodudu.cc/news/show-1873863.html

相关文章:

  • 人工智能ai应用高管指南_解决AI中的种族偏见:好奇心指南
  • 人工智能ai以算法为基础_IT团队如何为AI项目奠定正确的基础
  • ai人工智能_AI偏见如何发生?
  • unityui计分_铅计分成长
  • ml工程师_ML工程师正在失业。 仍然学习ML
  • ai智能和大数据测试_测试版可帮助您根据自己的条件创建数据和AI平台
  • ai人工智能_毕竟人工智能可能不适合您
  • gpt-2 文章自动生成_有助于您理解GPT-3的文章
  • 科技公司亚马逊名字由来_名字叫什么? 为什么亚马逊的“认可”是可爱营销的灾难性尝试
  • 无人驾驶 ai算法_AI机器学习具有碳足迹,因此无人驾驶汽车也是如此
  • 讲个故事,曾祖父
  • ai审计_用于内部审计和风险管理的人工智能
  • 自动化编程 ai_人工智能,自动化和音乐
  • 机器学习--线性回归1_线性回归-进入迷人世界的第一步
  • 神经网络 神经元_神经去耦
  • ai人工智能将替代人类_人类可以信任AI吗?
  • ai人工智能可以干什么_人工智能可以解决我的业务问题吗?
  • 如何识别媒体偏见_面部识别软件:宝贵资产,还是社会偏见的体现?
  • snorkel_Snorkel AI:标记培训数据的程序化方法
  • ai/ml_本月有关AI / ML的令人印象深刻的中等文章
  • ai人工智能最新相关消息_我如何了解最新的AI研究
  • 人工智能算法自动化测试_自动化:算法如何塑造我和你的生活
  • 情书,由多士炉写。
  • 快二游戏数据分析_1.更快的数据分析
  • 决策树人工智能预测模型_部署和服务AI模型进行预测的10种方法
  • 商业洞察力_正在进行的寻求洞察力和远见卓识
  • 阿里ai布局开始_如何从AI开始?
  • python惰性_如何创建惰性属性以提高Python的性能
  • 如何识别媒体偏见_面部识别技术存在偏见:为什么我们不应该盲目相信新技术
  • 自然语言处理:简单解释

keras pytorch_使用PyTorch重新创建Keras功能API相关推荐

  1. 线性嵌入 pytorch_使用Pytorch从头开始创建您的迷你单词嵌入

    线性嵌入 pytorch 机器学习 (Machine Learning) 介绍: (Introduction:) On a lighter note, the embedding of a parti ...

  2. 在PyTorch上用Keras,分布式训练开箱即用,告别没完没了的Debug

    鱼羊 发自 凹非寺  量子位 报道 | 公众号 QbitAI 在开始一个新的机器学习项目时,难免要重新编写训练循环,加载模型,分布式训练--然后在Debug的深渊里看着时间哗哗流逝,而自己离项目核心还 ...

  3. 【吴恩达课后编程作业pytorch实现】Keras入门与残差网络的搭建【1】

    直接上全部代码. 几个注意点: 整体的代码是在Colab上写的,前面因为要导入数据,所以引入一些了用不到的包. .该代码主要是根据原keras的实现代码改变而来,因为框架的不同,做了几点改动,改动中比 ...

  4. c调用python keras模型_使用TensorFlow 2.0创建Keras模型的三种方法

    TensorFlow 2.0和tf.keras提供了三种方式来实现神经网络模型:Sequential API Functional API Model subclassing 下面我将分别使用这三种方 ...

  5. keras时间序列数据预测_使用Keras的时间序列数据中的异常检测

    keras时间序列数据预测 Anomaly Detection in time series data provides e-commerce companies, finances the insi ...

  6. 标准化Keras:TensorFlow 2.0中的高级API指南

    TensorFlow正准备发布2.0版本 . 在本文中,我们希望预览TensorFlow的高级API标题的方向,并回答一些常见问题. Keras是一个非常受欢迎的高级API,用于构建和培训深度学习模型 ...

  7. python3.7安装keras教程_keras教程-02-tensorflow和keras安装

    声明: 本文由DataScience编辑发表, 转载请注明本文链接mlln.cn, 并在文后留言转载. 本文代码运行环境: windows10 python3.6 jupyter notebook t ...

  8. keras神经网络回归预测_如何使用Keras建立您的第一个神经网络来预测房价

    keras神经网络回归预测 by Joseph Lee Wei En 通过李维恩 一步一步的完整的初学者指南,可使用像Deep Learning专业版这样的几行代码来构建您的第一个神经网络! (A s ...

  9. keras构建卷积神经网络_在Keras中构建,加载和保存卷积神经网络

    keras构建卷积神经网络 This article is aimed at people who want to learn or review how to build a basic Convo ...

  10. Keras官方中文文档:Keras安装和配置指南(Windows)

    这里需要说明一下,笔者不建议在Windows环境下进行深度学习的研究,一方面是因为Windows所对应的框架搭建的依赖过多,社区设定不完全:另一方面,Linux系统下对显卡支持.内存释放以及存储空间调 ...

最新文章

  1. 报Java面授班有哪些优势
  2. 《编码:隐匿在计算机软硬件背后的语言(美)》读书笔记三
  3. GeoAnalyticsServer在Linux下集群部署手册
  4. java类使用其他类的变量_如何将java类中的变量加载到其他类
  5. hadoop学习记录
  6. CentOS 6.5 安装配置Tomcat7服务器
  7. Java----日期算法(计算两个date类型的时间差)
  8. 利用iisnode模块,让你的Node.js应用跑在Windows系统IIS中
  9. 20155339 《信息安全系统设计基础》课程总结
  10. PROTUES实例——stm32点灯
  11. c语言实验报告问题错误分析,C语言实验报告(三)
  12. FireBase Android版本测试
  13. Memtest移植到海思上面测试DDR
  14. 漏洞挖掘 符号执行_漏洞挖掘综述
  15. centos7.9安装zabbix+添加局域网下其他客户机
  16. (Cys-RGD)包被CdTe量子|3-巯基丙酸(MPA)包被近红外发光CdTe量子
  17. 人社部通知!2020年社保基数大调整、公积金必须同时缴纳、医保账户取消…
  18. Mysql为什么使用B+树(一)之红黑树简述
  19. 阿里入局,通义千问备受期待
  20. RMAN Encrypted Backups

热门文章

  1. OpenGL ES着色器语言之变量和数据类型(一)(官方文档第四章)和varying,uniform,attribute修饰范围...
  2. LINUX 下 一些常用的信息显示命令:
  3. SAP安装前应准备的事项
  4. vuforia for unity 注意事项
  5. 20200812每日一句
  6. Atitit 搜索与搜索领域的技术总结 目录 1. 搜索资料 各种文档累 office eml pdf zip 1 1.1. 搜索用的东东。。Es ffmpeg opencv 1 2. 自然语言处
  7. Atitit 嵌入式系统与pc系统的对比 目录 1. 哈佛结构和冯诺依曼结构 普林斯顿结构区 1 2. 中断程序 类库调用 1 3. 指令集 三大流程语句 与 运算语句 赋值语句 1 4. 异
  8. Atitit 剪贴板数据类型 DataFlavor 目录 1. HtmlFlavor 1 1.1. allHtmlFlavor 1 1.2. selectionHtmlFlavor 1 1.3. fr
  9. Atitit springboot 上传与下载总结 上传 使用file对象的transferTo保存方法最简单 @RequestMapping(value = /up, method = R
  10. linux:云端 ubuntu下挂载数据盘