On January 12, Keras writer Francois Chollet, for example, tweeted that he had started a Chinese document project for Keras on GitHub, as the pair received large attention from Chinese readers. Yesterday, Francois Chollet‏ again tweeted that the official Keras document was Indonesian! He would like to thank the translators and proofreaders for their hard work for over two months and hopes that Keras Chinese users will continue to help improve the quality of the document.
This release is the official Keras Chinese document, which has been carefully proofread to improve the overall quality. But the project is still a work in progress, and while there are a lot of API documentation and tutorials online, there are still a few things that are not complete. There were developers who built Keras’s Chinese documentation long before the official Chinese documentation existed, and many readers are using the Chinese documentation built by MoyanZitto and others.

  • Keras official documentation: https://keras.io/zh/
  • Keras third party documents: http://keras-cn.readthedocs.io/en/latest/
Here is a brief overview of the official Keras documentation.

Keras is a high-level neural network API written in Python that can run as a back end with TensorFlow, CNTK, or Theano. The focus of Keras development is to support rapid experimentation. Being able to translate your ideas into experimental results with minimal delay is the key to doing good research.



Please select Keras if you have the following requirements:

  • Allows simple and fast prototyping (user friendly, highly modular, extensible).
  • Both convolutional neural networks and recurrent neural networks are supported, as well as their combination.
  • Seamlessly run and switch on CPU and GPU.
Keras compatible versions of Python: Python 2.7-3.6.

Keras is relatively easy to build compared to other deep learning libraries: first, it provides a consistent and simple API; Second, it provides independent, fully configurable modules to form sequences or diagrams to complete the model; Finally, new modules are easy to extend as new classes and functions. This may sound abstract, but as the documentation describes, we were able to get up and running with Keras in 30 seconds. So those of you hovering outside the pit or preparing to go in can have fun with your 30 seconds.



Quick start: 30 seconds hand Keras

The core data structure of Keras is the Model, a way of organizing the network layer. The simplest model is the Sequential model, which is a stack of multiple network layers stacked linearly. For more complex structures, you should use the Keras functional API, which allows you to build arbitrary neural network diagrams.

The Sequential model is as follows:

from keras.models import Sequential

model = Sequential()Copy the code
You can simply stack models with.add() :

from keras.layers import Dense

model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))Copy the code
After the model is built, you can configure the learning process with.compile() :

model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])Copy the code
You can further configure the optimizer if necessary. One of the core tenets of Keras is to keep things fairly simple while allowing users complete control when they need it (the ultimate control is extensibility of source code).

Model.com from running (loss = keras. Losses. Categorical_crossentropy, optimizer = keras. Optimizers. SGD (lr = 0.01, momentum = 0.9, nesterov=True))Copy the code
Now you can iterate over training data in batches:

# x_train and y_train are Numpy arrays --just like in the Scikit-Learn API.
model.fit(x_train, y_train, epochs=5, batch_size=32)Copy the code
Alternatively, you can manually feed batch data to the model:

model.train_on_batch(x_batch, y_batch)Copy the code
A single line of code can evaluate model performance:

loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)Copy the code
Or generate predictions for new data:

classes = model.predict(x_test, batch_size=128)Copy the code
Building a question and answer system, an image classification model, a neural Turing machine, or any other model, is just that fast. The idea behind deep learning is simple, so why should their implementation be so painful?



Use profile

The use of Keras models can generally be divided into Sequential models, which are linear stacks of multiple network layers, and Keras functional apis, which are methods for defining complex models such as multi-output models, directed acyclic graphs, or models with shared layers. The following is a brief introduction to the use of the two models:



1.Keras sequential model

You can create a Sequential model by passing a list of layers to the Sequential constructor:

from keras.models import Sequential
from keras.layers import Dense, Activation

model = Sequential([
    Dense(32, input_shape=(784,)),
    Activation('relu'),
    Dense(10),
    Activation('softmax'),])Copy the code
Layers can also be added to the model using the.add() method:

model = Sequential()
model.add(Dense(32, input_dim=784))
model.add(Activation('relu'))Copy the code
A complete model of softmax multi-classification based on Multi-layer Perceptron (MLP) is shown below:

import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD

Generate virtual data
import numpy as np
x_train = np.random.random((1000, 20))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)
x_test = np.random.random((100, 20))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)

model = Sequential()
# Dense(64) is a fully connected layer with 64 hidden neurons.
At the first level you must specify the desired input data size, in this case a 20-dimensional vector.
model.add(Dense(64, activation='relu', Input_DIM =20)) Model.add (Dropout(0.5)) Model.add (Dense(64, activation='relu')) model. The add (Dropout (0.5)) model. The add (Dense (10, activation ='softmax') SGD = SGD(LR =0.01, decay= 1E-6, Momentum =0.9, nesterov=True) Model.compile (Loss ='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])

model.fit(x_train, y_train,
          epochs=20,
          batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)Copy the code


2. Keras functional API

With functional apis, you can easily reuse trained models: you can think of any model as a layer and then call it by passing in a tensor. Notice that when you invoke the model, you reuse not only the structure of the model, but also its weights.

Here’s a good example of a functional API: a model with multiple inputs and outputs. Functional apis make it easy to deal with large, interwoven data streams.

Consider the following model. We tried to predict how many retweets and likes a news headline would get on Twitter. The main input to the model will be the news headline itself, which is a set of words, but to spice things up, our model has added other auxiliary inputs to receive additional data, such as the date of publication of the news headline. The model will also use two loss functions for supervised learning. Early use of master loss functions in models is a good canonical approach for deep learning models.

The model structure is shown in the figure below:

Let’s implement it with a functional API (see the Chinese documentation for a full explanation) :

from keras.layers import Input, Embedding, LSTM, Dense
from keras.models import Model

# title input: Receives a sequence of 100 integers, each between 1 and 10000.
# Note that we can name any layer by passing a 'name' parameter.
main_input = Input(shape=(100,), dtype='int32', name='main_input')

The Embedding layer encodes the input sequence as a sequence of dense vectors with 512 dimensions.
x = Embedding(output_dim=512, input_dim=10000, input_length=100)(main_input)

The # LSTM layer converts a sequence of vectors into a single vector that contains context information for the entire sequence
lstm_out = LSTM(32)(x)

auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out)

auxiliary_input = Input(shape=(5,), name='aux_input')
x = keras.layers.concatenate([lstm_out, auxiliary_input])

Stack multiple fully connected network layers
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)

Finally add the main logistic regression layer
main_output = Dense(1, activation='sigmoid', name='main_output')(x)

model = Model(inputs=[main_input, auxiliary_input], outputs=[main_output, auxiliary_output])

model.compile(optimizer='rmsprop', loss='binary_crossentropy',
              loss_weights=[1., 0.2])


model.fit([headline_data, additional_data], [labels, labels],
          epochs=50, batch_size=32)

model.compile(optimizer='rmsprop',
              loss={'main_output': 'binary_crossentropy'.'aux_output': 'binary_crossentropy'},
              loss_weights={'main_output': 1.'aux_output': 0.2})

# Then use the following:
model.fit({'main_input': headline_data, 'aux_input': additional_data},
          {'main_output': labels, 'aux_output': labels},
          epochs=50, batch_size=32)Copy the code


This is just a simple example, but there are many more examples of the Keras functional API, including state-of-the-art visual models such as hierarchical sharing, directed acyclic graphs, and residual networks. Read on to learn more in the Chinese documentation.

The latter part of the document describes more commonly used functions and apis in Keras, including Keras models, hierarchical functions, preprocessing, loss functions, optimization methods, data sets, and visualization. These apis and corresponding functions can be found in the actual use of many times, of course, the most basic API we still need to understand. The Keras model and hierarchy API are briefly described below, and other modules are referred to the original Chinese documentation.



Keras model

There are two types of models in Keras, the sequential Model and the Model class Model using a functional API. These models have many approaches in common:

  • Model.summary (): Prints out the model overview information. It is a short call to utils.print_summary.
  • Model.get_config (): Returns a dictionary containing model configuration information. With the following code, you can re-instantiate the model based on this configuration information:
config = model.get_config()
model = Model.from_config(config)
# or, for Sequential:
model = Sequential.from_config(config)Copy the code
  • Model.get_weights (): Returns a list of tensors for model weights of type Numpy array.
  • Model.set_weights (weights): Sets weights for the model from the Nympy array. The arrays in the list must have the same size as the weights returned by get_weights().
  • Model.to_json (): Returns a representation of the model as a JSON string. Note that this representation does not include weights, only structures. You can re-instantiate the same model (with reinitialization weights) from a JSON string using the following code:
from keras.models import model_from_json

json_string = model.to_json()
model = model_from_json(json_string)Copy the code
  • Model.to_yaml (): Returns a representation of the model as a YAML string. Note that this representation does not include weights, only structures. You can re-instantiate the same model (with reinitialization weights) from a YAML string with the following code:
from keras.models import model_from_yaml

yaml_string = model.to_yaml()
model = model_from_yaml(yaml_string)Copy the code
  • Model.save_weights (filepath): Stores model weights as HDF5 files.
  • Model.load_weights (filepath, by_name=False): Loads the weights from the HDF5 file (created by save_weights). By default, the structure of the model should be constant. If you want to load weights into different models (some layers are the same), set by_name=True to load weights for layers with the same name.


Keras hierarchy

All Keras layers have many functions in common:

  • Layer.get_weights (): Returns the weights of layers in the form of a Numpy matrix.
  • Layer.set_weights (weights): Sets the weights of the layers from the Numpy matrix (the same output shape as get_weights).
  • Layer.get_config (): Returns a dictionary containing the layer configuration. This layer can be reset by:
layer = Dense(32)
config = layer.get_config()
reconstructed_layer = Dense.from_config(config)Copy the code
If a layer has a single node (i.e. if it is not a shared layer), you can get its input tensor, output tensor, input size and output size:

  • layer.input
  • layer.output
  • layer.input_shape
  • layer.output_shape
If the layer has multiple nodes, you can use the following functions:

  • layer.get_input_at(node_index)
  • layer.get_output_at(node_index)
  • layer.get_input_shape_at(node_index)
  • layer.get_output_shape_at(node_index)
These are the basic functions of the Keras model and hierarchy, and the core of the documentation is this section and the API usage and parameters described below. It includes the modules required for the complete model, including data, preprocessing, network architecture, training, evaluation, and visualization. But we won’t cover this section, because most of the time we will only look at it in detail when we encounter an unknown function.

Keras official Chinese document, welcome to wander into the pit.