Keras是一个用Python编写的深度学习API,运行在机器学习平台TensorFlow之上。

Ramesh Sampath cdb89b47d1 Align `model.summary`. Fix for long names and nested levels. 5 days ago
.devcontainer 52a2ba7c6e Use the official vscode Python image as the base image for devcontainer, instead 3 months ago
.github d2dbd69474 Enable manually triggering the code format check GitHub action. 2 weeks ago
.vscode 52a2ba7c6e Use the official vscode Python image as the base image for devcontainer, instead 3 months ago
keras cdb89b47d1 Align `model.summary`. Fix for long names and nested levels. 5 days ago
shell 43c52e4790 Make OSS scripts to be executable. 9 months ago
third_party f3f23fb6f5 Internal change 1 year ago
.bazelrc d18da3feba Internal Code Change 6 days ago
.bazelversion d41e008faa Pin the bazel version used by keras OSS build. 3 months ago
.gitignore 5513cf1e73 gitignore .DS_Store 1 year ago
BUILD 373ad97c72 Copy image utils from keras_preprocessing directly into core keras 1 year ago
CONTRIBUTING.md 29e3f12ed3 Add `-oss_excluded` to TF build/test tag filters 1 week ago
ISSUE_TEMPLATE.md 216da11938 Add issue template for keras-team/keras. 1 year ago
LICENSE ac893d1fa7 Change the `LICENSE` file to be a verbatim copy of the complete Apache 2.0 license: 1 year ago
README.md 8cbe696230 Update the Keras README 1 month ago
WORKSPACE 8331e3b0c8 feat: Update protobuf version to match latest TF protobuf update 1 month ago
oss_setup.py e2ce1cd802 Add script to build pip package that only shows the public API. 4 weeks ago
pip_build.py 2dcab061e2 Add script to automate Keras release. 3 weeks ago
requirements.txt 7a2639d8ed Remove the version tie for tb-nightly in requiremnets 1 month ago
setup.cfg 4b0c7f791b ignore flake8 W605 for invalid skip character. 1 month ago

README.md

Keras: Deep Learning for humans

Keras logo

This repository hosts the development of the Keras library. Read the documentation at keras.io.

About Keras

Keras is a deep learning API written in Python, running on top of the machine learning platform TensorFlow. It was developed with a focus on enabling fast experimentation and providing a delightful developer experience.

The purpose of Keras is to give an unfair advantage to any developer looking to ship ML-powered apps.

Keras is:

  • Simple -- but not simplistic. Keras reduces developer cognitive load to free you to focus on the parts of the problem that really matter. Keras focuses on ease of use, debugging speed, code elegance & conciseness, maintainability, and deployability (via TFServing, TFLite, TF.js).
  • Flexible -- Keras adopts the principle of progressive disclosure of complexity: simple workflows should be quick and easy, while arbitrarily advanced workflows should be possible via a clear path that builds upon what you've already learned.
  • Powerful -- Keras provides industry-strength performance and scalability: it is used by organizations and companies including NASA, YouTube, and Waymo. That's right -- your YouTube recommendations are powered by Keras, and so is the world's most advanced driverless vehicle.

Keras & TensorFlow 2

TensorFlow 2 is an end-to-end, open-source machine learning platform. You can think of it as an infrastructure layer for differentiable programming. It combines four key abilities:

  • Efficiently executing low-level tensor operations on CPU, GPU, or TPU.
  • Computing the gradient of arbitrary differentiable expressions.
  • Scaling computation to many devices, such as clusters of hundreds of GPUs.
  • Exporting programs ("graphs") to external runtimes such as servers, browsers, mobile and embedded devices.

Keras is the high-level API of TensorFlow 2: an approachable, highly-productive interface for solving machine learning problems, with a focus on modern deep learning. It provides essential abstractions and building blocks for developing and shipping machine learning solutions with high iteration velocity.

Keras empowers engineers and researchers to take full advantage of the scalability and cross-platform capabilities of TensorFlow 2: you can run Keras on TPU or on large clusters of GPUs, and you can export your Keras models to run in the browser or on a mobile device.


First contact with Keras

The core data structures of Keras are layers and models. 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 you to build arbitrary graphs of layers or write models entirely from scratch via subclassing.

Here is the Sequential model:

from tensorflow.keras.models import Sequential

model = Sequential()

Stacking layers is as easy as .add():

from tensorflow.keras.layers import Dense

model.add(Dense(units=64, activation='relu'))
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. The Keras philosophy is to keep simple things 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 via subclassing).

model.compile(loss=tf.keras.losses.categorical_crossentropy,
              optimizer=tf.keras.optimizers.SGD(
                  learning_rate=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.
model.fit(x_train, y_train, epochs=5, batch_size=32)

Evaluate your test loss and metrics 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)

What you just saw is the most elementary way to use Keras.

However, Keras is also a highly-flexible framework suitable to iterate on state-of-the-art research ideas. Keras follows the principle of progressive disclosure of complexity: it makes it easy to get started, yet it makes it possible to handle arbitrarily advanced use cases, only requiring incremental learning at each step.

In much the same way that you were able to train & evaluate a simple neural network above in a few lines, you can use Keras to quickly develop new training procedures or exotic model architectures. Here's a low-level training loop example, combining Keras functionality with the TensorFlow GradientTape:

import tensorflow as tf

# Prepare an optimizer.
optimizer = tf.keras.optimizers.Adam()
# Prepare a loss function.
loss_fn = tf.keras.losses.kl_divergence

# Iterate over the batches of a dataset.
for inputs, targets in dataset:
    # Open a GradientTape.
    with tf.GradientTape() as tape:
        # Forward pass.
        predictions = model(inputs)
        # Compute the loss value for this batch.
        loss_value = loss_fn(targets, predictions)

    # Get gradients of loss wrt the weights.
    gradients = tape.gradient(loss_value, model.trainable_weights)
    # Update the weights of the model.
    optimizer.apply_gradients(zip(gradients, model.trainable_weights))

For more in-depth tutorials about Keras, you can check out:


Installation

Keras comes packaged with TensorFlow 2 as tensorflow.keras. To start using Keras, simply install TensorFlow 2. You can then import Keras as follows:

from tensorflow import keras

Release and compatibility

Keras has nightly releases (keras-nightly on PyPI) and stable releases (keras on PyPI). The nightly Keras releases are usually compatible with the corresponding version of the tf-nightly releases (e.g. keras-nightly==2.7.0.dev2021100607 should be used with tf-nightly==2.7.0.dev2021100607). We don't maintain backward compatibility for nightly releases. For stable releases, each Keras version maps to a specific stable version of TensorFlow.

The table below shows the compatibility version mapping between TensorFlow versions and Keras versions.

All the release branches can be found on GitHub.

All the release binaries can be found on Pypi.


Support

You can ask questions and join the development discussion:


Opening an issue

You can also post bug reports and feature requests (only) in GitHub issues.


Opening a PR

We welcome contributions! Before opening a PR, please read our contributor guide, and the API design guideline.