SciTech-BigDataAIML-Tensorflow-Keras是用在Tensorflow的高层API

[https://tensorflow.google.cn/guide/keras](Keras: The high-level API for TensorFlow)

https://tensorflow.google.cn/guide/keras

The core data structures of Keras are layers and models.

  • A layer is a simple input/output transformation,

  • and a model is a DAG(directed acyclic graph) of layers.

  • You can set the trainability of variables on and off for any reason, including freezing layers and variables during fine-tuning. Note: tf.Module is the base class for both tf.keras.layers.Layer and tf.keras.Model, so everything you come across here also applies in Keras. For historical compatibility reasons Keras layers do not collect variables from modules, so your models should use only modules or only Keras layers. However, the methods shown below for inspecting variables are the same in either case.
    By subclassing tf.Module, any tf.Variable or tf.Module instances assigned to this object's properties are automatically collected. This allows you to save and load variables, and also create collections of tf.Modules.

# All trainable variables
print("trainable variables:", simple_module.trainable_variables)
# Every variable
print("all variables:", simple_module.variables)
trainable variables: (,)
all variables: (, )
This is an example of a two-layer linear layer model made out of modules.

Layers

The tf.keras.layers.Layer class is the fundamental abstraction in Keras.
A Layer encapsulates a state (weights) and some computation(defined in the tf.keras.layers.Layer.call method).

  • Weights created by layers can be trainable or non-trainable.
  • Layers are recursively composable: If you assign a layer instance as an attribute of another layer, the outer layer will start tracking the weights created by the inner layer.
  • You can also use layers to handle data preprocessing tasks like normalization and text vectorization. Preprocessing layers can be included directly into a model, either during or after training, which makes the model portable.

Models

  • A model is an object that groups layers together and that can be trained on data.
  • The simplest type of model is the Sequential model, which is a linear stack of layers.
  • For more complex architectures, you can either use the Keras functional API, which lets you build arbitrary graphs of layers, or use subclassing to write models from scratch.

The tf.keras.Model class features built-in training and evaluation methods:

  • tf.keras.Model.fit: Trains the model for a fixed number of epochs.
  • tf.keras.Model.predict: Generates output predictions for the input samples.
  • tf.keras.Model.evaluate: Returns the loss and metrics values for the model; configured via the tf.keras.Model.compile method.

These methods give you access to the following built-in training features:

  • Callbacks. You can leverage built-in callbacks for early stopping, model checkpointing, and TensorBoard monitoring. You can also implement custom callbacks.
  • Distributed training. You can easily scale up your training to multiple GPUs, TPUs, or devices.
  • Step fusing. With the steps_per_execution argument in tf.keras.Model.compile, you can process multiple batches in a single tf.function call, which greatly improves device utilization on TPUs.
    For a detailed overview of how to use fit, see the training and evaluation guide. To learn how to customize the built-in training and evaluation loops, see Customizing what happens in fit().

Other APIs and tools

Keras provides many other APIs and tools for deep learning, including:

For a full list of available APIs, see the Keras API reference. To learn more about other Keras projects and initiatives, see The Keras ecosystem.

Next steps

To get started using Keras with TensorFlow, check out the following topics:

The Sequential model

The Functional API

Training & evaluation with the built-in methods
Making new layers and models via subclassing
Serialization and saving
Working with preprocessing layers
Customizing what happens in fit()
Writing a training loop from scratch
Working with RNNs
Understanding masking & padding
Writing your own callbacks
Transfer learning & fine-tuning
Multi-GPU and distributed training
To learn more about Keras, see the following topics at keras.io:

About Keras
Introduction to Keras for Engineers
Introduction to Keras for Researchers
Keras API reference
The Keras ecosystem
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licen

posted @ 2024-05-08 16:54  abaelhe  阅读(15)  评论(0)    收藏  举报