使用Keras API在Tensorflow中构建神经网络的3种方法

If you are going around, checking out different tutorials, doing Google searches, spending a lot of time on Stack Overflow about TensorFlow, you might have realized that there are a ton of different ways to build neural network models. This has been an issue for TensorFlow for a long time. It is almost like TensorFlow is trying to find its path towards a bright deep learning environment. Well if you think about it, this is exactly what is happening and this is pretty normal for a library in its version 2.x. Since TensorFlow is so far the most mature deep learning library on the market, this is basically the best you can get.

如果您四处走动,检查不同的教程,进行Google搜索,在关于TensorFlow的Stack Overflow上花费大量时间,您可能已经意识到,有很多不同的方法来构建神经网络模型。 长期以来,这一直是TensorFlow的问题。 就像TensorFlow试图寻找通往明亮的深度学习环境的道路一样。 好吧,如果您考虑一下,这就是正在发生的事情,这对于其2.x版的库来说是很正常的。 由于TensorFlow是迄今为止市场上最成熟的深度学习库,因此基本上这是您可以获得的最好的。

Keras-TensorFlow关系 (Keras-TensorFlow Relationship)

一点背景(A Little Background)

TensorFlow’s evolution into a deep learning platform did not happen overnight. Initially, TensorFlow marketed itself as a symbolic math library for dataflow programming across a range of tasks. Therefore, the value proposition that the TensorFlow initially offered was not a pure machine learning library. The goal was to create an efficient math library so that custom machine learning algorithms that are built on top of this efficient structure would train in a short amount of time with high accuracy.

TensorFlow向深度学习平台的演进并非一overnight而就。 最初,TensorFlow推销自己为一种符号数学库,用于跨各种任务的数据流编程。 因此,TensorFlow最初提供的价值主张不是纯粹的机器学习库。 目标是创建一个高效的数学库,以便在这种高效结构之上构建的自定义机器学习算法可以在短时间内以高精度进行训练。

However, building models from scratch with low-level APIs repetitively was not very ideal. So, François Chollet, a Google engineer, developed Keras, as a separate high-level deep learning library. Although Keras has been capable of running on top of different libraries such as TensorFlow, Microsoft Cognitive Toolkit, Theano, or PlaidML, TensorFlow was and still is the most common library that people use Keras with.

但是,反复使用低级API从头开始构建模型不是很理想。 因此,Google工程师FrançoisChollet开发了Keras,作为一个单独的高级深度学习库。 尽管Keras能够在TensorFlow,Microsoft Cognitive Toolkit,Theano或PlaidML等不同的库之上运行,但TensorFlow一直是并且仍然是人们与Keras一起使用的最常见的库。

现在的情况 (Current Situation)

After seeing the messiness around the model-building process, the TensorFlow team announced that Keras is going to be the central high-level API used to build and train models in TensorFlow 2.0. The alternative high-level API, the Estimator API, has started to lose its already-diminishing popularity after this announcement.

在看到围绕模型构建过程的混乱之后,TensorFlow团队宣布Keras将成为TensorFlow 2.0中用于构建和训练模型的中央高级API。 此公告发布后,替代的高级API(估算器API )已开始失去其已经逐渐减少的知名度。

Estimator API和Keras API (The Estimator API and The Keras API)

使用Keras API在Tensorflow中构建神经网络的3种方法_第1张图片
Figure 2. The Positions of the Keras API and the Estimator API within TensorFlow Diagram 图2. TensorFlow图中Keras API和Estimator API的位置

Now, let’s go back to the problem: There are a lot of different methods that people build their models using TensorFlow. The main reason for this problem is TensorFlow’s failure to adopt a single Model API.

现在,让我们回到问题所在:人们使用TensorFlow建立模型的方法很多。 造成此问题的主要原因是TensorFlow无法采用单个Model API。

In version 1.x, for the production-level projects, the go-to model-building API was the Estimator API. But, with the recent changes, the Keras API has almost caught up with the Estimator API. Initially, the Estimator API was more scaleable, allowed multi-distribution, and had a convenient cross-platform functionality. Yet, most of these advantages of the Estimator API are now eliminated, and therefore, soon the Keras API will probably become the single standard API to build TensorFlow models.

在版本1.x中,对于生产级项目,模型构建API是Estimator API。 但是,随着最近的更改,Keras API几乎赶上了Estimator API。 最初,Estimator API具有更高的可扩展性,允许进行多分布以及具有便利的跨平台功能。 但是,现在已消除了Estimator API的大多数优点,因此,不久之后,Keras API可能会成为构建TensorFlow模型的单一标准API。

So, in this post, we will only focus on the Keras API methods to build models in TensorFlow and there are three of them:

因此,在本文中,我们将仅着重于在TensorFlow中构建模型的Keras API方法,其中包括三个:

  • Using the Sequential API

    使用顺序API
  • Using the Functional API

    使用功能性API
  • Model Subclassing

    模型子类化

I will make their comparison directly with their corresponding model building codes so that you can actually test them yourself. Let’s dive into coding.

我将直接将它们与相应的模型构建代码进行比较,以便您可以实际对其进行测试。 让我们深入研究编码。

初始代码进行比较 (Initial Code to Make the Comparison)

To test these three Keras methods, we need to select a deep learning problem. Image Classification with MNIST is a very straightforward task. What we are trying to achieve is training a model to recognize handwritten digits, using the famous MNIST dataset.

要测试这三种Keras方法,我们需要选择一个深度学习问题。 使用MNIST进行图像分类是一项非常简单的任务。 我们试图实现的目标是使用著名的MNIST数据集训练一个模型来识别手写数字。

使用Keras API在Tensorflow中构建神经网络的3种方法_第2张图片
Figure 3. Our Dummy Task for Benchmark Analysis: MNIST Image Classification 图3.我们用于基准分析的虚拟任务:MNIST图像分类

MNIST dataset, which stands for Modified National Institute of Standards and Technology database, is a large database of handwritten digits that is commonly used for training various image processing systems. The MNIST database contains 60,000 training images and 10,000 testing images taken from American Census Bureau employees and American high school students. You can find my separate tutorial about Image Classification if you would like to follow the full tutorial.

MNIST数据集(代表改良的美国国家标准技术研究所的数据库)是一个大型的手写数字数据库,通常用于训练各种图像处理系统。 MNIST数据库包含60,000张训练图像和10,000张测试图像,这些图像是从美国人口普查局员工和美国高中学生那里拍摄的。 如果您想阅读完整的教程,可以找到我关于图像分类的单独教程。

With the code below, we will import all layers and models so that it would not bother us in the upcoming parts. We also download the MNIST dataset and preprocess it so that it can be used in all models we will build with these three different methods. Just run the code below:

使用下面的代码,我们将导入所有层和模型,以免在以后的部分中困扰我们。 我们还下载了MNIST数据集并对其进行了预处理,以便可以在将要用这三种不同方法构建的所有模型中使用它。 只需运行以下代码:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras import Input
from tensorflow.keras import Model
from tensorflow.estimator import DNNClassifier
from tensorflow.keras.datasets.mnist import load_data


(x_train, y_train), (x_test, y_test)= load_data( path="mnist.npz" )


# Making sure that the values are float so that we can get decimal points after division
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# Normalizing the RGB codes by dividing it to the max RGB value.
x_train /= 255
x_test /= 255

Now, this part is out of the way, let’s focus on the three methods to build TensorFlow models.

现在,这部分已经不重要了,让我们关注构建TensorFlow模型的三种方法。

建立Keras模型的3种方法 (3 Ways to Build a Keras Model)

There are three methods to build a Keras model in TensorFlow:

在TensorFlow中使用三种方法来构建Keras模型:

  • The Sequential API: The Sequential API is the best method when you are trying to build a simple model with a single input, output, and layer branch. It is an excellent option for newcomers who would like to learn fast.

    顺序API:当您尝试构建具有单个输入,输出和层分支的简单模型时,顺序API是最好的方法。 对于想快速学习的新手来说,这是一个绝佳的选择。

  • The Functional API: The Functional API is the most popular method to build Keras models. It can do everything that the Sequential API can do. Also, it allows multiple inputs, multiple outputs, branching, and layer sharing. It is a clean and easy-to-use method, and it still allows a good level of customization flexibility.

    功能API:功能API是构建Keras模型的最流行的方法。 它可以完成顺序API可以完成的所有工作。 而且,它允许多个输入,多个输出,分支和层共享。 这是一种干净且易于使用的方法,并且仍然可以提供较高级别的自定义灵活性。

  • Model Subclassing: Model subclassing is for advanced level developers who need full control over their model, layer, and training process. You need to create a custom class defining the model, and you probably won’t need it for daily tasks. But, if you are a researcher with experimental needs, then model subclassing might be the best option for you since it would give you all the flexibility you need.

    模型子类:模型子类适用于需要完全控制其模型,层和培训过程的高级开发人员。 您需要创建一个定义模型的自定义类,并且日常工作中可能不需要它。 但是,如果您是有实验需求的研究人员,则模型子类化可能是您的最佳选择,因为它可以为您提供所需的所有灵活性。

使用Keras API在Tensorflow中构建神经网络的3种方法_第3张图片
Figure 4. Converting 2-Dim Image Array into 1 Dim Array with Flatten Layer 图4.将2-Dim图像阵列转换为具有平坦层的1 Dim阵列

Let’s see how these methods are implemented. We will build a basic feedforward neural network with a single Flatten layer to convert 2-dimensional image arrays to 1-dimensional arrays and two Dense layers.

让我们看看如何实现这些方法。 我们将构建一个具有单个Flatten层的基本前馈神经网络,以将2维图像数组转换为1维数组和2个Dense层。

顺序API (The Sequential API)

In the Sequential API, we need to create a Sequential object from tf.keras.Models module. We can simply pass all the layers as a single argument in the list format as shown below. As you can see that it is very simple.

在Sequential API中,我们需要从tf.keras.Models模块创建一个Sequential对象。 我们可以简单地以列表格式将所有图层作为单个参数传递,如下所示。 如您所见,这非常简单。

model = Sequential([ 
          Flatten(input_shape=(28, 28)), 
          Dense(256,'relu'), 
          Dense(10, "softmax"),
          ])

功能性API(The Functional API)

With Functional API, we need to define our input separately. Then, we need to create an output object by also creating all the layers which are tied to one another and to the output. Finally, we create a Model object which would accept inputs and outputs as arguments. The code is still very clean, but we have much more flexibility in the Functional API.

使用Functional API,我们需要分别定义输入。 然后,我们还需要通过创建相互关联并与输出绑定的所有图层来创建输出对象。 最后,我们创建一个Model对象,该对象将接受输入和输出作为参数。 代码仍然很干净,但是我们在Functional API中具有更大的灵活性。

inputs = Input(shape=(28, 28)) 
x = Flatten()(inputs) 
x = Dense(256, "relu")(x) 
outputs = Dense(10, "softmax")(x) 


model = Model(inputs=inputs, outputs=outputs, name="mnist_model")

模型子类化(Model Subclassing)

Let’s move on to model subclassing. In model subclassing, we start with creating a class extending tf.keras.Model class. There are two crucial functions in Model subclassing:

让我们继续进行模型子类化。 在模型子类化中,我们首先创建一个扩展tf.keras.Model类的类。 模型子类有两个关键功能:

  • __init__ function acts as a constructor. Thanks to __init__, we can initialize the attributes (e.g., layers) of our model. super is used calls the parent constructor (the constructor in tf.keras.Model) and self is used to refer to instance attributes (e.g., layers).

    __在里面__ 函数充当构造函数。 多亏__init__ ,我们可以初始化模型的属性(例如,layers )。 super用于调用父构造函数( tf.keras.Model中的构造函数),而self用于引用实例属性(例如,layers )。

  • call function is where the operations are defined after the layers are defined in the __init__ function.

    调用函数是在__init__函数中定义层之后定义操作的地方。

To build the same model with Model Subclassing, we need to write much more code, as shown below:

要使用Model Subclassing构建相同的模型,我们需要编写更多代码,如下所示:

class CustomModel(tf.keras.Model):


  def __init__(self, **kwargs):
    super(CustomModel, self).__init__(**kwargs)
    self.layer_1 = Flatten()
    self.layer_2 = Dense(256, "relu")
    self.layer_3 = Dense(10, "softmax")


  def call(self, inputs):
    x = self.layer_1(inputs)
    x = self.layer_2(x)
    x = self.layer_3(x)
    return x


model = CustomModel(name='mnist_model')

结束码(The End Code)

Now that you can create the same model with three different methods, you can choose any single one of them, build the model, and run the code below.

现在,您可以使用三种不同的方法创建相同的模型,您可以选择其中任意一种,构建模型,然后运行下面的代码。

model.compile(optimizer='adam', 
              loss='sparse_categorical_crossentropy', 
              metrics=['accuracy'])


model.fit(x=x_train,y=y_train, epochs=10)


model.evaluate(x_test, y_test)

The lines above take care of model configuration, training, and evaluation. When we compare the performances of these three methods, we see that they are pretty close but slightly different.

上面的几行内容涉及模型配置,培训和评估。 当我们比较这三种方法的性能时,我们发现它们非常接近,但略有不同。

使用Keras API在Tensorflow中构建神经网络的3种方法_第4张图片
Photo by Jonathan Chng on Unsplash Jonathan Chng在 Unsplash上的 照片

Our more complex Model Subclassing method outperforms the Sequential API and the Functional API. This shows that there are slight differences in the design of these methods in the low-end as well. However, these differences are negligible.

我们更复杂的Model Subclassing方法优于Sequential API和Functional API。 这表明在低端,这些方法的设计也略有不同。 但是,这些差异可以忽略不计。

最终评估 (Final Evaluations)

By now, you have an idea about the similarities and differences between these three Keras methods. But, let’s summarize them all in a table:

到目前为止,您已经对这三种Keras方法之间的异同有所了解。 但是,让我们在一个表中总结它们:

In summary, if you are just starting out, stick with the Sequential API. As you dive into more complex models, try out the Functional API. If you are doing a Ph.D. or just enjoy conducting independent research, try out Model Subclassing. If you are a professional, stick with the Functional API. It will probably satisfy your needs.

总而言之,如果您刚刚起步,请坚持使用顺序API。 当您进入更复杂的模型时,请尝试使用Functional API。 如果您正在攻读博士学位。 或只是喜欢进行独立研究,请尝试模型子分类。 如果您是专业人士,请坚持使用Functional API。 它可能会满足您的需求。

Let me know your favorite method to build Keras models in the comments.

在评论中让我知道您最喜欢的构建Keras模型的方法。

订阅邮件列表以获取完整代码 (Subscribe to the Mailing List for the Full Code)

If you would like to have access to full code on Google Colab and have access to my latest content, subscribe to the mailing list: ✉️

如果您想访问Google Colab上的完整代码并访问我的最新内容,请订阅邮件列表:✉️

演示地址

Slide to Subscribe 滑动以订阅

查看其他文章(Check Out Other Articles)

If you liked this article, check out some of my other articles:

如果您喜欢这篇文章,请查看我的其他一些文章:

翻译自: https://towardsdatascience.com/3-ways-to-build-neural-networks-in-tensorflow-with-the-keras-api-80e92d3b5b7e

你可能感兴趣的:(tensorflow,python,人工智能,深度学习,神经网络)