Deep Learning With Keras 简明教程
Deep Learning with Keras - Importing Libraries
我们首先导入项目代码所需的各种库。
We first import the various libraries required by the code in our project.
Array Handling and Plotting
通常,我们将 numpy 用于数组处理并将 matplotlib 用于绘图。在我们的项目中,通过以下 import 语句导入了这些库
As typical, we use numpy for array handling and matplotlib for plotting. These libraries are imported in our project using the following import statements
import numpy as np
import matplotlib
import matplotlib.pyplot as plot
Suppressing Warnings
由于 Tensorflow 和 Keras 会持续修改,如果您未在项目中同步它们的适当版本,在运行时您会看到许多警告错误。为了让您可以专心学习而不被这些错误分散注意力,我们将抑制该项目中的所有警告。这是通过以下几行代码完成的:
As both Tensorflow and Keras keep on revising, if you do not sync their appropriate versions in the project, at runtime you would see plenty of warning errors. As they distract your attention from learning, we shall be suppressing all the warnings in this project. This is done with the following lines of code −
# silent all warnings
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='3'
import warnings
warnings.filterwarnings('ignore')
from tensorflow.python.util import deprecation
deprecation._PRINT_DEPRECATION_WARNINGS = False
Keras
我们使用 Keras 库导入数据集。我们将使用 mnist 手写数字数据集。我们使用以下语句导入所需的软件包
We use Keras libraries to import dataset. We will use the mnist dataset for handwritten digits. We import the required package using the following statement
from keras.datasets import mnist
我们将使用 Keras 软件包定义我们的深度学习神经网络。我们导入 Sequential, Dense, Dropout 和 Activation 软件包来定义网络架构。我们使用 load_model 软件包来保存并检索我们的模型。我们还使用 np_utils 来获得项目中需要的几个实用工具。这些导入通过以下程序语句完成:
We will be defining our deep learning neural network using Keras packages. We import the Sequential, Dense, Dropout and Activation packages for defining the network architecture. We use load_model package for saving and retrieving our model. We also use np_utils for a few utilities that we need in our project. These imports are done with the following program statements −
from keras.models import Sequential, load_model
from keras.layers.core import Dense, Dropout, Activation
from keras.utils import np_utils
运行此代码时,您将在控制台上看到一条消息,表明 Keras 在后端使用 TensorFlow。此阶段的屏幕截图如下所示:
When you run this code, you will see a message on the console that says that Keras uses TensorFlow at the backend. The screenshot at this stage is shown here −
现在,由于我们已经有了项目所需的所有导入,我们将继续为我们的深度学习网络定义架构。
Now, as we have all the imports required by our project, we will proceed to define the architecture for our Deep Learning network.