Keras: The Python Deep Learning library Introduction

Posted Joey-Zhang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Keras: The Python Deep Learning library Introduction相关的知识,希望对你有一定的参考价值。

Keras: The Python Deep Learning library Introduction


Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano. It was developed with a focus on enabling fast experimentation. Being able to go from idea to result with the least possible delay is key to doing good research.

Use Keras if you need a deep learning library that:

  1. Allows for easy and fast prototyping (through user friendliness, modularity, and extensibility).
  2. Supports both convolutional networks and recurrent networks, as well as combinations of the two.
  3. Runs seamlessly on CPU and GPU.

Keras is compatible with: Python 2.7-3.6.

Getting started: 30 seconds to Keras

The core data structure of Keras is a model, a way to organize layers. The simplest type of model is the Sequential model, a linear stack of layers. For more complex architectures, you should use the Keras functional API, which allows to build arbitrary graphs of layers.

Here is the Sequential model:

from keras.models import Sequential

model = Sequential()

Stacking layers is as easy as .add():

from keras.layers import Dense

model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))

Once your model looks good, configure its learning process with .compile():

model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])

If you need to, you can further configure your optimizer. A core principle of Keras is to make things reasonably simple, while allowing the user to be fully in control when they need to (the ultimate control being the easy extensibility of the source code).

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True))

You can now iterate on your 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)

Alternatively, you can feed batches to your model manually:

model.train_on_batch(x_batch, y_batch)

Evaluate your performance in one line:

loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)

Or generate predictions on 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 as fast. The ideas behind deep learning are simple, so why should their implementation be painful?

Keras prioritizes developer experience

1. Keras is an API designed for human beings, not machines. Keras follows best practices for reducing cognitive load.
2.This makes Keras easy to learn and easy to use. As a Keras user, you are more productive, allowing you to try more ideas than your competition, faster – which in turn helps you win machine learning competitions.
3.This ease of use does not come at the cost of reduced flexibility.

以上是关于Keras: The Python Deep Learning library Introduction的主要内容,如果未能解决你的问题,请参考以下文章

Keras: The Python Deep Learning library Introduction

Keras学习手册,开篇-使用 Python3 的Deep Learning 库

How to Grid Search Hyperparameters for Deep Learning Models in Python With Keras

Building a Keras + deep learning REST API(三部曲之一)

Python深度学习 deep learning with Python 人民邮电出版社

C++中用frugally-deep读取keras的模型并进行预测(详细)