Keras 简明教程

Keras - Backend Configuration

本章详细解释了Keras后端实施TensorFlow和Theano。让我们逐一了解每个实施。

This chapter explains Keras backend implementations TensorFlow and Theano in detail. Let us go through each implementation one by one.

TensorFlow

TensorFlow 是 Google 开发的用于数字计算任务的开源机器学习库。Keras 是建立在 TensorFlow 或 Theano 之上的一个高级别 API。我们已经知道如何使用 pip 安装 TensorFlow。

TensorFlow is an open source machine learning library used for numerical computational tasks developed by Google. Keras is a high level API built on top of TensorFlow or Theano. We know already how to install TensorFlow using pip.

如果没有安装,可以使用以下命令安装:

If it is not installed, you can install using the below command −

pip install TensorFlow

一旦执行 Keras,我们就能够看到配置文件位于你的主目录中,进入 .keras/keras.json。

Once we execute keras, we could see the configuration file is located at your home directory inside and go to .keras/keras.json.

keras.json

{
   "image_data_format": "channels_last",
   "epsilon": 1e-07, "floatx": "float32", "backend": "tensorflow"
}

在此,

Here,

  1. image_data_format represent the data format.

  2. epsilon represents numeric constant. It is used to avoid DivideByZero error.

  3. float*x represent the default data type *float32. You can also change it to float16 or float64 using set_floatx() method.

  4. image_data_format represent the data format.

假设如果没有创建这个文件,那么就移动到相应位置,然后使用以下步骤创建:

Suppose, if the file is not created then move to the location and create using the below steps −

> cd home
> mkdir .keras
> vi keras.json

记住,你应该将其文件夹名称指定为 .keras,并在 keras.json 文件中添加以上配置。我们可以执行一些预定义的操作以了解后端函数。

Remember, you should specify .keras as its folder name and add the above configuration inside keras.json file. We can perform some pre-defined operations to know backend functions.

Theano

Theano 是一个开源深度学习库,允许你高效地计算多维数组。我们可以使用以下命令轻松安装:

Theano is an open source deep learning library that allows you to evaluate multi-dimensional arrays effectively. We can easily install using the below command −

pip install theano

默认情况下,Keras 使用 TensorFlow 后端。如果你想把后端配置从 TensorFlow 更改为 Theano,只需在 keras.json 文件中将 backend = theano 更改一下即可。如下所述:

By default, keras uses TensorFlow backend. If you want to change backend configuration from TensorFlow to Theano, just change the backend = theano in keras.json file. It is described below −

keras.json

{
   "image_data_format": "channels_last",
   "epsilon": 1e-07,
   "floatx": "float32",
   "backend": "theano"
}

现在,保存你的文件,重启终端并启动 Keras,你的后端将会被更改。

Now save your file, restart your terminal and start keras, your backend will be changed.

>>> import keras as k
using theano backend.