Keras
Keras,Python version of the TensorFlow deep learning API
Official Keras website, TensorFlow deep learning API in Python
What is Keras?
Keras is a high-level neural networks API, written in Python, that runs on top of TensorFlow, CNTK, or Theano. Keras focuses on enabling fast experimentation. The ability to convert your ideas into experimental results with minimal delay is crucial for good research.
Official Keras website:https://keras.io/zh/
Use Keras when you need a deep learning library in the following situations:
Enables simple and rapid prototyping (due to user-friendliness, high modularity, and scalability).
Supports both convolutional neural networks and recurrent neural networks, as well as their combinations.
Runs seamlessly on both CPU and GPU.
Guiding Principles
User-friendly. Keras is designed for humans, not machines. It prioritizes user experience. Keras follows best practices for reducing cognitive difficulties: it provides a consistent and simple API, minimizes the number of user operations required for common use cases, and offers clear and actionable feedback when errors occur.
Modular. Models are understood as sequences or graphs composed of independent, fully configurable modules. These modules can be assembled with as few constraints as possible. Especially neural network layers, loss functions, optimizers, initialization methods, activation functions, and regularization methods, which are all modules that can be combined to build new models.
Extensibility. New modules are easy to add (as new classes and functions), and existing modules have provided ample examples. Keras is particularly suitable for advanced research due to its ability to easily create new modules that can enhance performance.
Based on Python implementation. Keras does not have a separate configuration file with a specific format. Model definitions are written in Python code, which is compact, easy to debug, and easy to extend.
Quick Start: Get up and running with Keras in 30 seconds
Keras's core data structure is the model, a way to organize network layers. The simplest model is the Sequential model, which stacks multiple network layers linearly. For more complex structures, you should use Keras's functional API, which allows the construction of arbitrary neural network graphs.
The Sequential model is as follows:
from keras.models import Sequential
model = Sequential()
You can simply use .add() to stack the model:
from keras.layers import Dense
model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))
After completing the model's construction, you can use .compile() to configure the learning process:
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
If needed, you can further configure your optimizer. Keras's core principle is to make things quite simple while still allowing users to have complete control when necessary (ultimate control is the extensibility of the source code).
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True))
Now, you can iterate over the 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)
Or, you can manually provide batches of data to the model:
model.train_on_batch(x_batch, y_batch)
You can evaluate the model's performance with just one line of code:
loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)
Or generate predictions for new data:
classes = model.predict(x_test, batch_size=128)
Building a question-answering 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 its implementation be so painful?
For more in-depth tutorials on Keras, please see:
Get started with Sequential models
Get started with functional API
In the examples directory of the code repository, you will find more advanced models: question-answering systems based on memory networks, text generation based on stacked LSTM, and more.