keras内容查询

keras内容查询,第1张

1.python中的package和module

参考博文:https://blog.csdn.net/weixin_39788792
一、两个概念:package和module

package,就是一个包,里面可以装东西的,可以装subpackage,也可以装module。怎么确定一个文件夹就是一个package呢:关键就看这个package里面是否有一个py文件叫__init__.py。只要有这个文件,就把该文件作为一个package就可以了,不管是package还是subpackage。

那什么是module呢?就是package中的一个py文件。
二、init.py文件的作用

(1). init.py文件定义了包的属性和方法,

(2). init.py会告诉python这是一个package

(3). 如果在__init__.py文件中定义一个__all__ = [“Module1”, “Module2”, “subPackage1”, “subPackage2”]的变量,那么在import的时候,就会把__all__ 列表中的子模块和子包导入到当前作用域中来。

三、在开发的时候如何import?

  1. 常见的import有如下形式
    import package
    import module
    from package import module/subpackage (as XXX)
    from package.subpackage import module/subpackage (as XXX)
    from module import function
    from package import * ##

但是,import module.function这种写法是错误的。
在调用包的时候,

(1)同一个main_package下的各个subpackage相互调用。如sub_package1中的module1.py想调用sub_package1的module2.py、sub_package1的module2.py中的function1函数和sub_package2的module2.py

那么可以这样使用:
from . import module2.py
from .module2.py import function1
from …sub_package2 import module2.py
总结:“. ”是导入本package的module,“… ”是导入与package同级的packge中的module

(2)不同的main_package下的module.py文件相互调用。这个就比较常见了,不多讲。

2.keras 与tensorflow.keras的区别和联系

参考博文:原文链接:https://blog.csdn.net/puran1218/article/details/104421179

keras和tensorflow、theano都是深度学习框架,不同的是keras没有自己的后端,依托tensorflow、theano等为后端进行运行,keras被看做是一个封装的高级API。那么有小伙伴也许会问为什么会出现keras呢,直接用tensorflow不行吗,这里就涉及到了keras的优点,作为封装的高阶API,keras更对用户友好,比tensorflow、theano更易学。
keras的调用:
keras有两个版本:支持多后端(backend)的Keras和tf.keras

2.1 Multi-backend Keras

为了训练你自己的自定义神经网络,Keras 需要一个后端,即一个计算引擎——它可以构建网络的图和拓扑结构,运行优化器,并执行具体的数字运算。这个后端可以是TensorFlow、CNTK或Theano。
在 v1.1.0 之前,Keras 的默认后端都是 Theano。 v1.1.0 之后,TensorFlow开始成为 Keras 的默认后端。

2.2 tf.keras

tf.keras 是在 TensorFlow v1.10.0 中引入的,此时tf.keras 软件包与通过 pip 安装的 keras 软件包是分开的。在TensorFlow 2.0 中, tf.keras 是 TensorFlow 的官方高阶 API,用于快速简单的模型设计和训练。
Keras v2.3.0 是 Keras 第一个与 tf.keras 同步的版本,也将是最后一个支持多个后端的主要版本。之后,Multi-backend Keras 要被tf.keras替代了。
官方的建议也是希望用户从多后端keras转向tf.keras

3.keras库

以下内容参考了此篇文章,需要的小伙伴可以移步一下哦,讲的巨好。 原文链接:https://blog.csdn.net/lk7688535/article/details/52862377

多后端keras和tf.keras包含的包的内容稍有差别,此处咱们用tf.keras为例,版本tensorflow ==2.3.0, tf.keras == 2.3.1

NAME
    tensorflow.keras - Implementation of the Keras API meant to be a high-level API for TensorFlow.

DESCRIPTION
    Detailed documentation and user guides are available at
    [tensorflow.org](https://www.tensorflow.org/guide/keras).

PACKAGE CONTENTS
    activations (package)
    applications (package)
    backend (package)
    callbacks (package)
    constraints (package)
    datasets (package)
    estimator (package)
    experimental (package)
    initializers (package)
    layers (package)
    losses (package)
    metrics (package)
    mixed_precision (package)
    models (package)
    optimizers (package)
    premade (package)
    preprocessing (package)
    regularizers (package)
    utils (package)
    wrappers (package)

VERSION
    2.4.0

3.1 models:

models中的方法和属性:Sequential/Model/clone_model/model_from_config/model_from_json/model_from_json/load_model/save_model

from tensorflow.python.keras.engine.sequential import Sequential
from tensorflow.python.keras.engine.training import Model
from tensorflow.python.keras.models import clone_model
from tensorflow.python.keras.saving.model_config import model_from_config
from tensorflow.python.keras.saving.model_config import model_from_json
from tensorflow.python.keras.saving.model_config import model_from_yaml
from tensorflow.python.keras.saving.save import load_model
from tensorflow.python.keras.saving.save import save_model

models层是最主要的模块,models层可以将上面定义了各种基本组件组合起来
keras有两种model,分别是Sequential模型和泛型模型。

model的方法:
model.summary() : 打印出模型概况
model.get_config() :返回包含模型配置信息的Python字典
model.get_weights():返回模型权重张量的列表,类型为numpy array
model.set_weights():从numpy array里将权重载入给模型
model.to_json:返回代表模型的JSON字符串,仅包含网络结构,不包含权值。可用于重构原模型
model.to_yaml:与model.to_json类似,同样可以从产生的YAML字符串中重构模型。
model.save_weights(filepath):将模型权重保存到指定路径,文件类型是HDF5(后缀是.h5)
model.load_weights(filepath, by_name=False):从HDF5文件中加载权重到当前模型中, 默认情况下模型的结构将保持不变。如果想将权重载入不同的模型(有些层相同)中,则设置by_name=True,只有名字匹配的层才会载入权重
help(sequential)可以查看sequential模型下都有哪些方法。
model.fit()
help(Sequential.fit)

Help on function fit in module tensorflow.python.keras.engine.training:

fit(self, x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None, validation_batch_size=None, validation_freq=1, max_queue_size=10, workers=1, use_multiprocessing=False)
    Trains the model for a fixed number of epochs (iterations on a dataset).
    
    Arguments:
        x: Input data. It could be:
          - A Numpy array (or array-like), or a list of arrays
            (in case the model has multiple inputs).
          - A TensorFlow tensor, or a list of tensors
            (in case the model has multiple inputs).
          - A dict mapping input names to the corresponding array/tensors,
            if the model has named inputs.
          - A `tf.data` dataset. Should return a tuple
            of either `(inputs, targets)` or
            `(inputs, targets, sample_weights)`.
          - A generator or `keras.utils.Sequence` returning `(inputs, targets)`
            or `(inputs, targets, sample_weights)`.
          A more detailed description of unpacking behavior for iterator types
          (Dataset, generator, Sequence) is given below.
        y: Target data. Like the input data `x`,
          it could be either Numpy array(s) or TensorFlow tensor(s).
          It should be consistent with `x` (you cannot have Numpy inputs and
          tensor targets, or inversely). If `x` is a dataset, generator,
          or `keras.utils.Sequence` instance, `y` should
          not be specified (since targets will be obtained from `x`).
        batch_size: Integer or `None`.
            Number of samples per gradient update.
            If unspecified, `batch_size` will default to 32.
            Do not specify the `batch_size` if your data is in the
            form of datasets, generators, or `keras.utils.Sequence` instances
            (since they generate batches).
        epochs: Integer. Number of epochs to train the model.
            An epoch is an iteration over the entire `x` and `y`
            data provided.
            Note that in conjunction with `initial_epoch`,
            `epochs` is to be understood as "final epoch".
            The model is not trained for a number of iterations
            given by `epochs`, but merely until the epoch
            of index `epochs` is reached.
        verbose: 0, 1, or 2. Verbosity mode.
            0 = silent, 1 = progress bar, 2 = one line per epoch.
            Note that the progress bar is not particularly useful when
            logged to a file, so verbose=2 is recommended when not running
            interactively (eg, in a production environment).
        callbacks: List of `keras.callbacks.Callback` instances.
            List of callbacks to apply during training.
            See `tf.keras.callbacks`.
        validation_split: Float between 0 and 1.
            Fraction of the training data to be used as validation data.
            The model will set apart this fraction of the training data,
            will not train on it, and will evaluate
            the loss and any model metrics
            on this data at the end of each epoch.
            The validation data is selected from the last samples
            in the `x` and `y` data provided, before shuffling. This argument is
            not supported when `x` is a dataset, generator or
           `keras.utils.Sequence` instance.
        validation_data: Data on which to evaluate
            the loss and any model metrics at the end of each epoch.
            The model will not be trained on this data. Thus, note the fact
            that the validation loss of data provided using `validation_split`
            or `validation_data` is not affected by regularization layers like
            noise and dropuout.
            `validation_data` will override `validation_split`.
            `validation_data` could be:
              - tuple `(x_val, y_val)` of Numpy arrays or tensors
              - tuple `(x_val, y_val, val_sample_weights)` of Numpy arrays
              - dataset
            For the first two cases, `batch_size` must be provided.
            For the last case, `validation_steps` could be provided.
            Note that `validation_data` does not support all the data types that
            are supported in `x`, eg, dict, generator or `keras.utils.Sequence`.
        shuffle: Boolean (whether to shuffle the training data
            before each epoch) or str (for 'batch'). This argument is ignored
            when `x` is a generator. 'batch' is a special option for dealing
            with the limitations of HDF5 data; it shuffles in batch-sized
            chunks. Has no effect when `steps_per_epoch` is not `None`.
        class_weight: Optional dictionary mapping class indices (integers)
            to a weight (float) value, used for weighting the loss function
            (during training only).
            This can be useful to tell the model to
            "pay more attention" to samples from
            an under-represented class.
        sample_weight: Optional Numpy array of weights for
            the training samples, used for weighting the loss function
            (during training only). You can either pass a flat (1D)
            Numpy array with the same length as the input samples
            (1:1 mapping between weights and samples),
            or in the case of temporal data,
            you can pass a 2D array with shape
            `(samples, sequence_length)`,
            to apply a different weight to every timestep of every sample. This
            argument is not supported when `x` is a dataset, generator, or
           `keras.utils.Sequence` instance, instead provide the sample_weights
            as the third element of `x`.
        initial_epoch: Integer.
            Epoch at which to start training
            (useful for resuming a previous training run).
        steps_per_epoch: Integer or `None`.
            Total number of steps (batches of samples)
            before declaring one epoch finished and starting the
            next epoch. When training with input tensors such as
            TensorFlow data tensors, the default `None` is equal to
            the number of samples in your dataset divided by
            the batch size, or 1 if that cannot be determined. If x is a
            `tf.data` dataset, and 'steps_per_epoch'
            is None, the epoch will run until the input dataset is exhausted.
            When passing an infinitely repeating dataset, you must specify the
            `steps_per_epoch` argument. This argument is not supported with
            array inputs.
        validation_steps: Only relevant if `validation_data` is provided and
            is a `tf.data` dataset. Total number of steps (batches of
            samples) to draw before stopping when performing validation
            at the end of every epoch. If 'validation_steps' is None, validation
            will run until the `validation_data` dataset is exhausted. In the
            case of an infinitely repeated dataset, it will run into an
            infinite loop. If 'validation_steps' is specified and only part of
            the dataset will be consumed, the evaluation will start from the
            beginning of the dataset at each epoch. This ensures that the same
            validation samples are used every time.
        validation_batch_size: Integer or `None`.
            Number of samples per validation batch.
            If unspecified, will default to `batch_size`.
            Do not specify the `validation_batch_size` if your data is in the
            form of datasets, generators, or `keras.utils.Sequence` instances
            (since they generate batches).
        validation_freq: Only relevant if validation data is provided. Integer
            or `collections_abc.Container` instance (e.g. list, tuple, etc.).
            If an integer, specifies how many training epochs to run before a
            new validation run is performed, e.g. `validation_freq=2` runs
            validation every 2 epochs. If a Container, specifies the epochs on
            which to run validation, e.g. `validation_freq=[1, 2, 10]` runs
            validation at the end of the 1st, 2nd, and 10th epochs.
        max_queue_size: Integer. Used for generator or `keras.utils.Sequence`
            input only. Maximum size for the generator queue.
            If unspecified, `max_queue_size` will default to 10.
        workers: Integer. Used for generator or `keras.utils.Sequence` input
            only. Maximum number of processes to spin up
            when using process-based threading. If unspecified, `workers`
            will default to 1. If 0, will execute the generator on the main
            thread.
        use_multiprocessing: Boolean. Used for generator or
            `keras.utils.Sequence` input only. If `True`, use process-based
            threading. If unspecified, `use_multiprocessing` will default to
            `False`. Note that because this implementation relies on
            multiprocessing, you should not pass non-picklable arguments to
            the generator as they can't be passed easily to children processes.
    
    Unpacking behavior for iterator-like inputs:
        A common pattern is to pass a tf.data.Dataset, generator, or
      tf.keras.utils.Sequence to the `x` argument of fit, which will in fact
      yield not only features (x) but optionally targets (y) and sample weights.
      Keras requires that the output of such iterator-likes be unambiguous. The
      iterator should return a tuple of length 1, 2, or 3, where the optional
      second and third elements will be used for y and sample_weight
      respectively. Any other type provided will be wrapped in a length one
      tuple, effectively treating everything as 'x'. When yielding dicts, they
      should still adhere to the top-level tuple structure.
      e.g. `({"x0": x0, "x1": x1}, y)`. Keras will not attempt to separate
      features, targets, and weights from the keys of a single dict.
        A notable unsupported data type is the namedtuple. The reason is that
      it behaves like both an ordered datatype (tuple) and a mapping
      datatype (dict). So given a namedtuple of the form:
          `namedtuple("example_tuple", ["y", "x"])`
      it is ambiguous whether to reverse the order of the elements when
      interpreting the value. Even worse is a tuple of the form:
          `namedtuple("other_tuple", ["x", "y", "z"])`
      where it is unclear if the tuple was intended to be unpacked into x, y,
      and sample_weight or passed through as a single element to `x`. As a
      result the data processing code will simply raise a ValueError if it
      encounters a namedtuple. (Along with instructions to remedy the issue.)
    
    Returns:
        A `History` object. Its `History.history` attribute is
        a record of training loss values and metrics values
        at successive epochs, as well as validation loss values
        and validation metrics values (if applicable).
    
    Raises:
        RuntimeError: 1. If the model was never compiled or,
        2. If `model.fit` is  wrapped in `tf.function`.
    
        ValueError: In case of mismatch between the provided input data
            and what the model expects.

model.evaluate()
help(Sequential.evaluate)

Help on function evaluate in module tensorflow.python.keras.engine.training:

evaluate(self, x=None, y=None, batch_size=None, verbose=1, sample_weight=None, steps=None, callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False, return_dict=False)
    Returns the loss value & metrics values for the model in test mode.
    
    Computation is done in batches (see the `batch_size` arg.)
    
    Arguments:
        x: Input data. It could be:
          - A Numpy array (or array-like), or a list of arrays
            (in case the model has multiple inputs).
          - A TensorFlow tensor, or a list of tensors
            (in case the model has multiple inputs).
          - A dict mapping input names to the corresponding array/tensors,
            if the model has named inputs.
          - A `tf.data` dataset. Should return a tuple
            of either `(inputs, targets)` or
            `(inputs, targets, sample_weights)`.
          - A generator or `keras.utils.Sequence` returning `(inputs, targets)`
            or `(inputs, targets, sample_weights)`.
          A more detailed description of unpacking behavior for iterator types
          (Dataset, generator, Sequence) is given in the `Unpacking behavior
          for iterator-like inputs` section of `Model.fit`.
        y: Target data. Like the input data `x`, it could be either Numpy
          array(s) or TensorFlow tensor(s). It should be consistent with `x`
          (you cannot have Numpy inputs and tensor targets, or inversely). If
          `x` is a dataset, generator or `keras.utils.Sequence` instance, `y`
          should not be specified (since targets will be obtained from the
          iterator/dataset).
        batch_size: Integer or `None`. Number of samples per batch of
          computation. If unspecified, `batch_size` will default to 32. Do not
          specify the `batch_size` if your data is in the form of a dataset,
          generators, or `keras.utils.Sequence` instances (since they generate
          batches).
        verbose: 0 or 1. Verbosity mode. 0 = silent, 1 = progress bar.
        sample_weight: Optional Numpy array of weights for the test samples,
          used for weighting the loss function. You can either pass a flat (1D)
          Numpy array with the same length as the input samples
            (1:1 mapping between weights and samples), or in the case of
              temporal data, you can pass a 2D array with shape `(samples,
              sequence_length)`, to apply a different weight to every timestep
              of every sample. This argument is not supported when `x` is a
              dataset, instead pass sample weights as the third element of `x`.
        steps: Integer or `None`. Total number of steps (batches of samples)
          before declaring the evaluation round finished. Ignored with the
          default value of `None`. If x is a `tf.data` dataset and `steps` is
          None, 'evaluate' will run until the dataset is exhausted. This
          argument is not supported with array inputs.
        callbacks: List of `keras.callbacks.Callback` instances. List of
          callbacks to apply during evaluation. See
          [callbacks](/api_docs/python/tf/keras/callbacks).
        max_queue_size: Integer. Used for generator or `keras.utils.Sequence`
          input only. Maximum size for the generator queue. If unspecified,
          `max_queue_size` will default to 10.
        workers: Integer. Used for generator or `keras.utils.Sequence` input
          only. Maximum number of processes to spin up when using process-based
          threading. If unspecified, `workers` will default to 1. If 0, will
          execute the generator on the main thread.
        use_multiprocessing: Boolean. Used for generator or
          `keras.utils.Sequence` input only. If `True`, use process-based
          threading. If unspecified, `use_multiprocessing` will default to
          `False`. Note that because this implementation relies on
          multiprocessing, you should not pass non-picklable arguments to the
          generator as they can't be passed easily to children processes.
        return_dict: If `True`, loss and metric results are returned as a dict,
          with each key being the name of the metric. If `False`, they are
          returned as a list.
    
    See the discussion of `Unpacking behavior for iterator-like inputs` for
    `Model.fit`.
    
    Returns:
        Scalar test loss (if the model has a single output and no metrics)
        or list of scalars (if the model has multiple outputs
        and/or metrics). The attribute `model.metrics_names` will give you
        the display labels for the scalar outputs.
    
    Raises:
        RuntimeError: If `model.evaluate` is wrapped in `tf.function`.
        ValueError: in case of invalid arguments.

model.predict()
help(Sequential.predict)

Help on function predict in module tensorflow.python.keras.engine.training:

predict(self, x, batch_size=None, verbose=0, steps=None, callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False)
    Generates output predictions for the input samples.
    
    Computation is done in batches. This method is designed for performance in
    large scale inputs. For small amount of inputs that fit in one batch,
    directly using `__call__` is recommended for faster execution, e.g.,
    `model(x)`, or `model(x, training=False)` if you have layers such as
    `tf.keras.layers.BatchNormalization` that behaves differently during
    inference. Also, note the fact that test loss is not affected by
    regularization layers like noise and dropout.
    
    Arguments:
        x: Input samples. It could be:
          - A Numpy array (or array-like), or a list of arrays
            (in case the model has multiple inputs).
          - A TensorFlow tensor, or a list of tensors
            (in case the model has multiple inputs).
          - A `tf.data` dataset.
          - A generator or `keras.utils.Sequence` instance.
          A more detailed description of unpacking behavior for iterator types
          (Dataset, generator, Sequence) is given in the `Unpacking behavior
          for iterator-like inputs` section of `Model.fit`.
        batch_size: Integer or `None`.
            Number of samples per batch.
            If unspecified, `batch_size` will default to 32.
            Do not specify the `batch_size` if your data is in the
            form of dataset, generators, or `keras.utils.Sequence` instances
            (since they generate batches).
        verbose: Verbosity mode, 0 or 1.
        steps: Total number of steps (batches of samples)
            before declaring the prediction round finished.
            Ignored with the default value of `None`. If x is a `tf.data`
            dataset and `steps` is None, `predict` will
            run until the input dataset is exhausted.
        callbacks: List of `keras.callbacks.Callback` instances.
            List of callbacks to apply during prediction.
            See [callbacks](/api_docs/python/tf/keras/callbacks).
        max_queue_size: Integer. Used for generator or `keras.utils.Sequence`
            input only. Maximum size for the generator queue.
            If unspecified, `max_queue_size` will default to 10.
        workers: Integer. Used for generator or `keras.utils.Sequence` input
            only. Maximum number of processes to spin up when using
            process-based threading. If unspecified, `workers` will default
            to 1. If 0, will execute the generator on the main thread.
        use_multiprocessing: Boolean. Used for generator or
            `keras.utils.Sequence` input only. If `True`, use process-based
            threading. If unspecified, `use_multiprocessing` will default to
            `False`. Note that because this implementation relies on
            multiprocessing, you should not pass non-picklable arguments to
            the generator as they can't be passed easily to children processes.
    
    See the discussion of `Unpacking behavior for iterator-like inputs` for
    `Model.fit`. Note that Model.predict uses the same interpretation rules as
    `Model.fit` and `Model.evaluate`, so inputs must be unambiguous for all
    three methods.
    
    Returns:
        Numpy array(s) of predictions.
    
    Raises:
        RuntimeError: If `model.predict` is wrapped in `tf.function`.
        ValueError: In case of mismatch between the provided
            input data and the model's expectations,
            or in case a stateful model receives a number of samples
            that is not a multiple of the batch size.

3.1.1 Sequential 序列模型

把多个网络层线性堆叠起来,可以通过向Sequential模型传递一个layer的list来构造该模型或使用add:

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(8))
model.add(tf.keras.layers.Dense(4))

or 

model=tf.keras.Sequential([
tf.keras.layers.Dense(8),
tf.keras.layers.Dense(4)
])

注意,当使用延迟构建模式(没有指定输入形状)时,当你第一次调用’ fit ', ’ eval ‘,或’ predict '时,或者当你第一次在某些输入数据上调用模型时,模型会被构建。
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(8))
model.add(tf.keras.layers.Dense(1))
model.compile(optimizer=‘sgd’, loss=‘mse’)
model.fit(x, y, batch_size=32, epoch =10) #第一次构建模型`

3.1.2 Model

待完善

3.1.4 model_from_json

读取json配置信息,返回一个模型实例:

>>> model = tf.keras.Sequential([
...     tf.keras.layers.Dense(5, input_shape=(3,)),
...     tf.keras.layers.Softmax()])
>>> config = model.to_json()
>>> loaded_model = tf.keras.models.model_from_json(config)
3.1.5 model_from_yaml

读取一个yaml框架信息文件(只是框架,没有权重),返回一个模型实例:

model=tf.keras.Sequential([
    tf.keras.layers.Dense(5,input_shape=(3,)),
    tf.keras.layers.Softmax()])

import yaml
config=model.to_yaml()
load_model=tf.keras.models.model_from_yaml(config)
3.1.6 load_model

load_model(filepath, custom_objects=None, compile=True, options=None)
加载save保存的模型,返回模型实例

>>> model = tf.keras.Sequential([
...     tf.keras.layers.Dense(5, input_shape=(3,)),
...     tf.keras.layers.Softmax()])
>>> model.save('/tmp/model')
>>> loaded_model = tf.keras.models.load_model('/tmp/model')
>>> x = tf.random.uniform((10, 3))
>>> assert np.allclose(model.predict(x), loaded_model.predict(x))


Arguments:
filepath: One of the following:
String or pathlib.Path object, path to the saved model

h5py.File object from which to load the model

custom_objects: Optional dictionary mapping names
(strings) to custom classes or functions to be considered during deserialization.

compile: Boolean, whether to compile the model
after loading.

options: Optional tf.saved_model.LoadOptions object that specifies
options for loading from SavedModel.

Returns:
A Keras model instance. If the original model was compiled, and saved with the optimizer, then the returned model will be compiled. Otherwise, the model will be left uncompiled. In the case that an uncompiled model is returned, a warning is displayed if the compile argument is set to True.

Raises:
ImportError: if loading from an hdf5 file and h5py is not available. IOError: In case of an invalid savefile.


请注意,在加载之后,模型权重可能有不同的作用域名称。作用域名称包括模型/层名称,例如“dense_1/kernel:0”。建议您使用layer属性来访问特定的变量,例如model.get_layer(“dense_1”).kernel。

3.1.7 save_model

save_model(model, filepath, overwrite=True, include_optimizer=True, save_format=None, signatures=None, options=None)
Saves a model as a TensorFlow SavedModel or HDF5 file.


Usage:

>>> model = tf.keras.Sequential([
...     tf.keras.layers.Dense(5, input_shape=(3,)),
...     tf.keras.layers.Softmax()])
>>> model.save('/tmp/model')
>>> loaded_model = tf.keras.models.load_model('/tmp/model')
>>> x = tf.random.uniform((10, 3))
>>> assert np.allclose(model.predict(x), loaded_model.predict(x))
The saved model contains:
the model's configuration (topology)
the model's weights
the model's optimizer's state (if any)
Thus the saved model can be reinstantiated in the exact same state, without any of the code used for model definition or training.
Note that the model weights may have different scoped names after being loaded. Scoped names include the model/layer names, such as "dense_1/kernel:0". It is recommended that you use the layer properties to access specific variables, e.g. model.get_layer("dense_1").kernel._SavedModel serialization_

The SavedModel serialization path uses tf.saved_model.save to save the model and all trackable objects attached to the model (e.g. layers and variables). @tf.function-decorated methods are also saved. Additional trackable objects and functions are added to the SavedModel to allow the model to be loaded back as a Keras Model object.

Arguments:
model: Keras model instance to be saved.
 filepath: One of the following:String or pathlib.Path object, path where to save the model h5py.File object where to save the model

overwrite: Whether we should overwrite any existing model at the target
location, or instead ask the user with a manual prompt.

include_optimizer: If True, save optimizer's state together. 
save_format: Either 'tf' or 'h5', indicating whether to save the model
to Tensorflow SavedModel or HDF5. Defaults to 'tf' in TF 2.X, and 'h5' in TF 1.X.

signatures: Signatures to save with the SavedModel. Applicable to the 'tf' format only. Please see the signatures argument in tf.saved_model.save for details.

options: Optional tf.saved_model.SaveOptions object that specifies
options for saving to SavedModel.

Raises:
ImportError: If save format is hdf5, and h5py is not available.
3.2 layers

layers层中包含的内容:

from . import experimental
from tensorflow.python.keras.engine.base_layer import Layer
from tensorflow.python.keras.engine.input_layer import Input
from tensorflow.python.keras.engine.input_layer import InputLayer
from tensorflow.python.keras.engine.input_spec import InputSpec
from tensorflow.python.keras.feature_column.dense_features_v2 import DenseFeatures
from tensorflow.python.keras.layers.advanced_activations import ELU
from tensorflow.python.keras.layers.advanced_activations import LeakyReLU
from tensorflow.python.keras.layers.advanced_activations import PReLU
from tensorflow.python.keras.layers.advanced_activations import ReLU
from tensorflow.python.keras.layers.advanced_activations import Softmax
from tensorflow.python.keras.layers.advanced_activations import ThresholdedReLU
from tensorflow.python.keras.layers.convolutional import Conv1D
from tensorflow.python.keras.layers.convolutional import Conv1D as Convolution1D
from tensorflow.python.keras.layers.convolutional import Conv1DTranspose
from tensorflow.python.keras.layers.convolutional import Conv1DTranspose as Convolution1DTranspose
from tensorflow.python.keras.layers.convolutional import Conv2D
from tensorflow.python.keras.layers.convolutional import Conv2D as Convolution2D
from tensorflow.python.keras.layers.convolutional import Conv2DTranspose
from tensorflow.python.keras.layers.convolutional import Conv2DTranspose as Convolution2DTranspose
from tensorflow.python.keras.layers.convolutional import Conv3D
from tensorflow.python.keras.layers.convolutional import Conv3D as Convolution3D
from tensorflow.python.keras.layers.convolutional import Conv3DTranspose
from tensorflow.python.keras.layers.convolutional import Conv3DTranspose as Convolution3DTranspose
from tensorflow.python.keras.layers.convolutional import Cropping1D
from tensorflow.python.keras.layers.convolutional import Cropping2D
from tensorflow.python.keras.layers.convolutional import Cropping3D
from tensorflow.python.keras.layers.convolutional import DepthwiseConv2D
from tensorflow.python.keras.layers.convolutional import SeparableConv1D
from tensorflow.python.keras.layers.convolutional import SeparableConv1D as SeparableConvolution1D
from tensorflow.python.keras.layers.convolutional import SeparableConv2D
from tensorflow.python.keras.layers.convolutional import SeparableConv2D as SeparableConvolution2D
from tensorflow.python.keras.layers.convolutional import UpSampling1D
from tensorflow.python.keras.layers.convolutional import UpSampling2D
from tensorflow.python.keras.layers.convolutional import UpSampling3D
from tensorflow.python.keras.layers.convolutional import ZeroPadding1D
from tensorflow.python.keras.layers.convolutional import ZeroPadding2D
from tensorflow.python.keras.layers.convolutional import ZeroPadding3D
from tensorflow.python.keras.layers.convolutional_recurrent import ConvLSTM2D
from tensorflow.python.keras.layers.core import Activation
from tensorflow.python.keras.layers.core import ActivityRegularization
from tensorflow.python.keras.layers.core import Dense
from tensorflow.python.keras.layers.core import Dropout
from tensorflow.python.keras.layers.core import Flatten
from tensorflow.python.keras.layers.core import Lambda
from tensorflow.python.keras.layers.core import Masking
from tensorflow.python.keras.layers.core import Permute
from tensorflow.python.keras.layers.core import RepeatVector
from tensorflow.python.keras.layers.core import Reshape
from tensorflow.python.keras.layers.core import SpatialDropout1D
from tensorflow.python.keras.layers.core import SpatialDropout2D
from tensorflow.python.keras.layers.core import SpatialDropout3D
from tensorflow.python.keras.layers.dense_attention import AdditiveAttention
from tensorflow.python.keras.layers.dense_attention import Attention
from tensorflow.python.keras.layers.embeddings import Embedding
from tensorflow.python.keras.layers.local import LocallyConnected1D
from tensorflow.python.keras.layers.local import LocallyConnected2D
from tensorflow.python.keras.layers.merge import Add
from tensorflow.python.keras.layers.merge import Average
from tensorflow.python.keras.layers.merge import Concatenate
from tensorflow.python.keras.layers.merge import Dot
from tensorflow.python.keras.layers.merge import Maximum
from tensorflow.python.keras.layers.merge import Minimum
from tensorflow.python.keras.layers.merge import Multiply
from tensorflow.python.keras.layers.merge import Subtract
from tensorflow.python.keras.layers.merge import add
from tensorflow.python.keras.layers.merge import average
from tensorflow.python.keras.layers.merge import concatenate
from tensorflow.python.keras.layers.merge import dot
from tensorflow.python.keras.layers.merge import maximum
from tensorflow.python.keras.layers.merge import minimum
from tensorflow.python.keras.layers.merge import multiply
from tensorflow.python.keras.layers.merge import subtract
from tensorflow.python.keras.layers.noise import AlphaDropout
from tensorflow.python.keras.layers.noise import GaussianDropout
from tensorflow.python.keras.layers.noise import GaussianNoise
from tensorflow.python.keras.layers.normalization import LayerNormalization
from tensorflow.python.keras.layers.normalization_v2 import BatchNormalization
from tensorflow.python.keras.layers.pooling import AveragePooling1D
from tensorflow.python.keras.layers.pooling import AveragePooling1D as AvgPool1D
from tensorflow.python.keras.layers.pooling import AveragePooling2D
from tensorflow.python.keras.layers.pooling import AveragePooling2D as AvgPool2D
from tensorflow.python.keras.layers.pooling import AveragePooling3D
from tensorflow.python.keras.layers.pooling import AveragePooling3D as AvgPool3D
from tensorflow.python.keras.layers.pooling import GlobalAveragePooling1D
from tensorflow.python.keras.layers.pooling import GlobalAveragePooling1D as GlobalAvgPool1D
from tensorflow.python.keras.layers.pooling import GlobalAveragePooling2D
from tensorflow.python.keras.layers.pooling import GlobalAveragePooling2D as GlobalAvgPool2D
from tensorflow.python.keras.layers.pooling import GlobalAveragePooling3D
from tensorflow.python.keras.layers.pooling import GlobalAveragePooling3D as GlobalAvgPool3D
from tensorflow.python.keras.layers.pooling import GlobalMaxPooling1D
from tensorflow.python.keras.layers.pooling import GlobalMaxPooling1D as GlobalMaxPool1D
from tensorflow.python.keras.layers.pooling import GlobalMaxPooling2D
from tensorflow.python.keras.layers.pooling import GlobalMaxPooling2D as GlobalMaxPool2D
from tensorflow.python.keras.layers.pooling import GlobalMaxPooling3D
from tensorflow.python.keras.layers.pooling import GlobalMaxPooling3D as GlobalMaxPool3D
from tensorflow.python.keras.layers.pooling import MaxPooling1D
from tensorflow.python.keras.layers.pooling import MaxPooling1D as MaxPool1D
from tensorflow.python.keras.layers.pooling import MaxPooling2D
from tensorflow.python.keras.layers.pooling import MaxPooling2D as MaxPool2D
from tensorflow.python.keras.layers.pooling import MaxPooling3D
from tensorflow.python.keras.layers.pooling import MaxPooling3D as MaxPool3D
from tensorflow.python.keras.layers.recurrent import AbstractRNNCell
from tensorflow.python.keras.layers.recurrent import RNN
from tensorflow.python.keras.layers.recurrent import SimpleRNN
from tensorflow.python.keras.layers.recurrent import SimpleRNNCell
from tensorflow.python.keras.layers.recurrent import StackedRNNCells
from tensorflow.python.keras.layers.recurrent_v2 import GRU
from tensorflow.python.keras.layers.recurrent_v2 import GRUCell
from tensorflow.python.keras.layers.recurrent_v2 import LSTM
from tensorflow.python.keras.layers.recurrent_v2 import LSTMCell
from tensorflow.python.keras.layers.serialization import deserialize
from tensorflow.python.keras.layers.serialization import serialize
from tensorflow.python.keras.layers.wrappers import Bidirectional
from tensorflow.python.keras.layers.wrappers import TimeDistributed
from tensorflow.python.keras.layers.wrappers import Wrapper

#常用层

3.2.1 Conv1D / Conv2D / Conv3D

查看:help(tensorflow.keras.layers.Conv1D)

Help on class Conv1D in module tensorflow.python.keras.layers.convolutional:

class Conv1D(Conv)
 |  Conv1D(*args, **kwargs)
 |  
 |  1D convolution layer (e.g. temporal convolution).
 |  
 |  This layer creates a convolution kernel that is convolved
 |  with the layer input over a single spatial (or temporal) dimension
 |  to produce a tensor of outputs.
 |  If `use_bias` is True, a bias vector is created and added to the outputs.
 |  Finally, if `activation` is not `None`,
 |  it is applied to the outputs as well.
 |  
 |  When using this layer as the first layer in a model,
 |  provide an `input_shape` argument
 |  (tuple of integers or `None`, e.g.
 |  `(10, 128)` for sequences of 10 vectors of 128-dimensional vectors,
 |  or `(None, 128)` for variable-length sequences of 128-dimensional vectors.
 |  
 |  Examples:
 |  
 |  >>> # The inputs are 128-length vectors with 10 timesteps, and the batch size
 |  >>> # is 4.
 |  >>> input_shape = (4, 10, 128)
 |  >>> x = tf.random.normal(input_shape)
 |  >>> y = tf.keras.layers.Conv1D(
 |  ... 32, 3, activation='relu',input_shape=input_shape[1:])(x)
 |  >>> print(y.shape)
 |  (4, 8, 32)
 |  
 |  >>> # With extended batch shape [4, 7] (e.g. weather data where batch
 |  >>> # dimensions correspond to spatial location and the third dimension
 |  >>> # corresponds to time.)
 |  >>> input_shape = (4, 7, 10, 128)
 |  >>> x = tf.random.normal(input_shape)
 |  >>> y = tf.keras.layers.Conv1D(
 |  ... 32, 3, activation='relu', input_shape=input_shape[2:])(x)
 |  >>> print(y.shape)
 |  (4, 7, 8, 32)
 |  
 |  Arguments:
 |    filters: Integer, the dimensionality of the output space
 |      (i.e. the number of output filters in the convolution).
 |    kernel_size: An integer or tuple/list of a single integer,
 |      specifying the length of the 1D convolution window.
 |    strides: An integer or tuple/list of a single integer,
 |      specifying the stride length of the convolution.
 |      Specifying any stride value != 1 is incompatible with specifying
 |      any `dilation_rate` value != 1.
 |    padding: One of `"valid"`, `"causal"` or `"same"` (case-insensitive).
 |      `"causal"` results in causal (dilated) convolutions, e.g. `output[t]`
 |      does not depend on `input[t+1:]`. Useful when modeling temporal data
 |      where the model should not violate the temporal order.
 |      See [WaveNet: A Generative Model for Raw Audio, section
 |        2.1](https://arxiv.org/abs/1609.03499).
 |    data_format: A string,
 |      one of `channels_last` (default) or `channels_first`.
 |    dilation_rate: an integer or tuple/list of a single integer, specifying
 |      the dilation rate to use for dilated convolution.
 |      Currently, specifying any `dilation_rate` value != 1 is
 |      incompatible with specifying any `strides` value != 1.
 |    groups: A positive integer specifying the number of groups in which the
 |      input is split along the channel axis. Each group is convolved
 |      separately with `filters / groups` filters. The output is the
 |      concatenation of all the `groups` results along the channel axis.
 |      Input channels and `filters` must both be divisible by `groups`.
 |    activation: Activation function to use.
 |      If you don't specify anything, no activation is applied (
 |      see `keras.activations`).
 |    use_bias: Boolean, whether the layer uses a bias vector.
 |    kernel_initializer: Initializer for the `kernel` weights matrix (
 |      see `keras.initializers`).
 |    bias_initializer: Initializer for the bias vector (
 |      see `keras.initializers`).
 |    kernel_regularizer: Regularizer function applied to
 |      the `kernel` weights matrix (see `keras.regularizers`).
 |    bias_regularizer: Regularizer function applied to the bias vector (
 |      see `keras.regularizers`).
 |    activity_regularizer: Regularizer function applied to
 |      the output of the layer (its "activation") (
 |      see `keras.regularizers`).
 |    kernel_constraint: Constraint function applied to the kernel matrix (
 |      see `keras.constraints`).
 |    bias_constraint: Constraint function applied to the bias vector (
 |      see `keras.constraints`).
 |  
 |  Input shape:
 |    3+D tensor with shape: `batch_shape + (steps, input_dim)`
 |  
 |  Output shape:
 |    3+D tensor with shape: `batch_shape + (new_steps, filters)`
 |      `steps` value might have changed due to padding or strides.
 |  
 |  Returns:
 |    A tensor of rank 3 representing
 |    `activation(conv1d(inputs, kernel) + bias)`.
 |  
 |  Raises:
 |    ValueError: when both `strides > 1` and `dilation_rate > 1`

Conv2D:

Help on class Conv2D in module tensorflow.python.keras.layers.convolutional:

class Conv2D(Conv)
 |  Conv2D(*args, **kwargs)
 |  
 |  2D convolution layer (e.g. spatial convolution over images).
 |  
 |  This layer creates a convolution kernel that is convolved
 |  with the layer input to produce a tensor of
 |  outputs. If `use_bias` is True,
 |  a bias vector is created and added to the outputs. Finally, if
 |  `activation` is not `None`, it is applied to the outputs as well.
 |  
 |  When using this layer as the first layer in a model,
 |  provide the keyword argument `input_shape`
 |  (tuple of integers, does not include the sample axis),
 |  e.g. `input_shape=(128, 128, 3)` for 128x128 RGB pictures
 |  in `data_format="channels_last"`.
 |  
 |  Examples:
 |  
 |  >>> # The inputs are 28x28 RGB images with `channels_last` and the batch
 |  >>> # size is 4.
 |  >>> input_shape = (4, 28, 28, 3)
 |  >>> x = tf.random.normal(input_shape)
 |  >>> y = tf.keras.layers.Conv2D(
 |  ... 2, 3, activation='relu', input_shape=input_shape[1:])(x)
 |  >>> print(y.shape)
 |  (4, 26, 26, 2)
 |  
 |  >>> # With `dilation_rate` as 2.
 |  >>> input_shape = (4, 28, 28, 3)
 |  >>> x = tf.random.normal(input_shape)
 |  >>> y = tf.keras.layers.Conv2D(
 |  ... 2, 3, activation='relu', dilation_rate=2, input_shape=input_shape[1:])(x)
 |  >>> print(y.shape)
 |  (4, 24, 24, 2)
 |  
 |  >>> # With `padding` as "same".
 |  >>> input_shape = (4, 28, 28, 3)
 |  >>> x = tf.random.normal(input_shape)
 |  >>> y = tf.keras.layers.Conv2D(
 |  ... 2, 3, activation='relu', padding="same", input_shape=input_shape[1:])(x)
 |  >>> print(y.shape)
 |  (4, 28, 28, 2)
 |  
 |  >>> # With extended batch shape [4, 7]:
 |  >>> input_shape = (4, 7, 28, 28, 3)
 |  >>> x = tf.random.normal(input_shape)
 |  >>> y = tf.keras.layers.Conv2D(
 |  ... 2, 3, activation='relu', input_shape=input_shape[2:])(x)
 |  >>> print(y.shape)
 |  (4, 7, 26, 26, 2)
 |  
 |  
 |  Arguments:
 |    filters: Integer, the dimensionality of the output space (i.e. the number of
 |      output filters in the convolution).
 |    kernel_size: An integer or tuple/list of 2 integers, specifying the height
 |      and width of the 2D convolution window. Can be a single integer to specify
 |      the same value for all spatial dimensions.
 |    strides: An integer or tuple/list of 2 integers, specifying the strides of
 |      the convolution along the height and width. Can be a single integer to
 |      specify the same value for all spatial dimensions. Specifying any stride
 |      value != 1 is incompatible with specifying any `dilation_rate` value != 1.
 |    padding: one of `"valid"` or `"same"` (case-insensitive).
 |    data_format: A string, one of `channels_last` (default) or `channels_first`.
 |      The ordering of the dimensions in the inputs. `channels_last` corresponds
 |      to inputs with shape `(batch_size, height, width, channels)` while
 |      `channels_first` corresponds to inputs with shape `(batch_size, channels,
 |      height, width)`. It defaults to the `image_data_format` value found in
 |      your Keras config file at `~/.keras/keras.json`. If you never set it, then
 |      it will be `channels_last`.
 |    dilation_rate: an integer or tuple/list of 2 integers, specifying the
 |      dilation rate to use for dilated convolution. Can be a single integer to
 |      specify the same value for all spatial dimensions. Currently, specifying
 |      any `dilation_rate` value != 1 is incompatible with specifying any stride
 |      value != 1.
 |    groups: A positive integer specifying the number of groups in which the
 |      input is split along the channel axis. Each group is convolved separately
 |      with `filters / groups` filters. The output is the concatenation of all
 |      the `groups` results along the channel axis. Input channels and `filters`
 |      must both be divisible by `groups`.
 |    activation: Activation function to use. If you don't specify anything, no
 |      activation is applied (see `keras.activations`).
 |    use_bias: Boolean, whether the layer uses a bias vector.
 |    kernel_initializer: Initializer for the `kernel` weights matrix (see
 |      `keras.initializers`).
 |    bias_initializer: Initializer for the bias vector (see
 |      `keras.initializers`).
 |    kernel_regularizer: Regularizer function applied to the `kernel` weights
 |      matrix (see `keras.regularizers`).
 |    bias_regularizer: Regularizer function applied to the bias vector (see
 |      `keras.regularizers`).
 |    activity_regularizer: Regularizer function applied to the output of the
 |      layer (its "activation") (see `keras.regularizers`).
 |    kernel_constraint: Constraint function applied to the kernel matrix (see
 |      `keras.constraints`).
 |    bias_constraint: Constraint function applied to the bias vector (see
 |      `keras.constraints`).
 |  Input shape:
 |    4+D tensor with shape: `batch_shape + (channels, rows, cols)` if
 |      `data_format='channels_first'`
 |    or 4+D tensor with shape: `batch_shape + (rows, cols, channels)` if
 |      `data_format='channels_last'`.
 |  Output shape:
 |    4+D tensor with shape: `batch_shape + (filters, new_rows, new_cols)` if
 |    `data_format='channels_first'` or 4+D tensor with shape: `batch_shape +
 |      (new_rows, new_cols, filters)` if `data_format='channels_last'`.  `rows`
 |      and `cols` values might have changed due to padding.
 |  
 |  Returns:
 |    A tensor of rank 4+ representing
 |    `activation(conv2d(inputs, kernel) + bias)`.
 |  
 |  Raises:
 |    ValueError: if `padding` is `"causal"`.
 |    ValueError: when both `strides > 1` and `dilation_rate > 1`.
 |  

Conv3D:

class Conv3D(Conv)
 |  Conv3D(*args, **kwargs)
 |  
 |  3D convolution layer (e.g. spatial convolution over volumes).
 |  
 |  This layer creates a convolution kernel that is convolved
 |  with the layer input to produce a tensor of
 |  outputs. If `use_bias` is True,
 |  a bias vector is created and added to the outputs. Finally, if
 |  `activation` is not `None`, it is applied to the outputs as well.
 |  
 |  When using this layer as the first layer in a model,
 |  provide the keyword argument `input_shape`
 |  (tuple of integers, does not include the sample axis),
 |  e.g. `input_shape=(128, 128, 128, 1)` for 128x128x128 volumes
 |  with a single channel,
 |  in `data_format="channels_last"`.
 |  
 |  Examples:
 |  
 |  >>> # The inputs are 28x28x28 volumes with a single channel, and the
 |  >>> # batch size is 4
 |  >>> input_shape =(4, 28, 28, 28, 1)
 |  >>> x = tf.random.normal(input_shape)
 |  >>> y = tf.keras.layers.Conv3D(
 |  ... 2, 3, activation='relu', input_shape=input_shape[1:])(x)
 |  >>> print(y.shape)
 |  (4, 26, 26, 26, 2)
 |  
 |  >>> # With extended batch shape [4, 7], e.g. a batch of 4 videos of 3D frames,
 |  >>> # with 7 frames per video.
 |  >>> input_shape = (4, 7, 28, 28, 28, 1)
 |  >>> x = tf.random.normal(input_shape)
 |  >>> y = tf.keras.layers.Conv3D(
 |  ... 2, 3, activation='relu', input_shape=input_shape[2:])(x)
 |  >>> print(y.shape)
 |  (4, 7, 26, 26, 26, 2)
 |  
 |  Arguments:
 |    filters: Integer, the dimensionality of the output space (i.e. the number of
 |      output filters in the convolution).
 |    kernel_size: An integer or tuple/list of 3 integers, specifying the depth,
 |      height and width of the 3D convolution window. Can be a single integer to
 |      specify the same value for all spatial dimensions.
 |    strides: An integer or tuple/list of 3 integers, specifying the strides of
 |      the convolution along each spatial dimension. Can be a single integer to
 |      specify the same value for all spatial dimensions. Specifying any stride
 |      value != 1 is incompatible with specifying any `dilation_rate` value != 1.
 |    padding: one of `"valid"` or `"same"` (case-insensitive).
 |    data_format: A string, one of `channels_last` (default) or `channels_first`.
 |      The ordering of the dimensions in the inputs. `channels_last` corresponds
 |      to inputs with shape `batch_shape + (spatial_dim1, spatial_dim2,
 |      spatial_dim3, channels)` while `channels_first` corresponds to inputs with
 |      shape `batch_shape + (channels, spatial_dim1, spatial_dim2,
 |      spatial_dim3)`. It defaults to the `image_data_format` value found in your
 |      Keras config file at `~/.keras/keras.json`. If you never set it, then it
 |      will be "channels_last".
 |    dilation_rate: an integer or tuple/list of 3 integers, specifying the
 |      dilation rate to use for dilated convolution. Can be a single integer to
 |      specify the same value for all spatial dimensions. Currently, specifying
 |      any `dilation_rate` value != 1 is incompatible with specifying any stride
 |      value != 1.
 |    groups: A positive integer specifying the number of groups in which the
 |      input is split along the channel axis. Each group is convolved separately
 |      with `filters / groups` filters. The output is the concatenation of all
 |      the `groups` results along the channel axis. Input channels and `filters`
 |      must both be divisible by `groups`.
 |    activation: Activation function to use. If you don't specify anything, no
 |      activation is applied (see `keras.activations`).
 |    use_bias: Boolean, whether the layer uses a bias vector.
 |    kernel_initializer: Initializer for the `kernel` weights matrix (see
 |      `keras.initializers`).
 |    bias_initializer: Initializer for the bias vector (see
 |      `keras.initializers`).
 |    kernel_regularizer: Regularizer function applied to the `kernel` weights
 |      matrix (see `keras.regularizers`).
 |    bias_regularizer: Regularizer function applied to the bias vector (see
 |      `keras.regularizers`).
 |    activity_regularizer: Regularizer function applied to the output of the
 |      layer (its "activation") (see `keras.regularizers`).
 |    kernel_constraint: Constraint function applied to the kernel matrix (see
 |      `keras.constraints`).
 |    bias_constraint: Constraint function applied to the bias vector (see
 |      `keras.constraints`).
 |  Input shape:
 |    5+D tensor with shape: `batch_shape + (channels, conv_dim1, conv_dim2,
 |      conv_dim3)` if data_format='channels_first'
 |    or 5+D tensor with shape: `batch_shape + (conv_dim1, conv_dim2, conv_dim3,
 |      channels)` if data_format='channels_last'.
 |  Output shape:
 |    5+D tensor with shape: `batch_shape + (filters, new_conv_dim1,
 |      new_conv_dim2, new_conv_dim3)` if data_format='channels_first'
 |    or 5+D tensor with shape: `batch_shape + (new_conv_dim1, new_conv_dim2,
 |      new_conv_dim3, filters)` if data_format='channels_last'. `new_conv_dim1`,
 |      `new_conv_dim2` and `new_conv_dim3` values might have changed due to
 |      padding.
 |  
 |  Returns:
 |    A tensor of rank 5+ representing
 |    `activation(conv3d(inputs, kernel) + bias)`.
 |  
 |  Raises:
 |    ValueError: if `padding` is "causal".
 |    ValueError: when both `strides > 1` and `dilation_rate > 1`.
3.2.2 MaxPooling1D / MaxPooling2D / MaxPooling3D

MaxPooling1D:
help(tensorflow.keras.layers.MaxPooling1D)

class MaxPooling1D(Pooling1D)
 |  MaxPooling1D(*args, **kwargs)
 |  
 |  Max pooling operation for 1D temporal data.
 |  
 |  Downsamples the input representation by taking the maximum value over the
 |  window defined by `pool_size`. The window is shifted by `strides`.  The
 |  resulting output when using "valid" padding option has a shape of:
 |  `output_shape = (input_shape - pool_size + 1) / strides)`
 |  
 |  The resulting output shape when using the "same" padding option is:
 |  `output_shape = input_shape / strides`
 |  
 |  For example, for strides=1 and padding="valid":
 |  
 |  >>> x = tf.constant([1., 2., 3., 4., 5.])
 |  >>> x = tf.reshape(x, [1, 5, 1])
 |  >>> max_pool_1d = tf.keras.layers.MaxPooling1D(pool_size=2,
 |  ...    strides=1, padding='valid')
 |  >>> max_pool_1d(x)
 |  <tf.Tensor: shape=(1, 4, 1), dtype=float32, numpy=
 |  array([[[2.],
 |          [3.],
 |          [4.],
 |          [5.]]], dtype=float32)>
 |  
 |  For example, for strides=2 and padding="valid":
 |  
 |  >>> x = tf.constant([1., 2., 3., 4., 5.])
 |  >>> x = tf.reshape(x, [1, 5, 1])
 |  >>> max_pool_1d = tf.keras.layers.MaxPooling1D(pool_size=2,
 |  ...    strides=2, padding='valid')
 |  >>> max_pool_1d(x)
 |  <tf.Tensor: shape=(1, 2, 1), dtype=float32, numpy=
 |  array([[[2.],
 |          [4.]]], dtype=float32)>
 |  
 |  For example, for strides=1 and padding="same":
 |  
 |  >>> x = tf.constant([1., 2., 3., 4., 5.])
 |  >>> x = tf.reshape(x, [1, 5, 1])
 |  >>> max_pool_1d = tf.keras.layers.MaxPooling1D(pool_size=2,
 |  ...    strides=1, padding='same')
 |  >>> max_pool_1d(x)
 |  <tf.Tensor: shape=(1, 5, 1), dtype=float32, numpy=
 |  array([[[2.],
 |          [3.],
 |          [4.],
 |          [5.],
 |          [5.]]], dtype=float32)>
 |  
 |  Arguments:
 |    pool_size: Integer, size of the max pooling window.
 |    strides: Integer, or None. Specifies how much the pooling window moves
 |      for each pooling step.
 |      If None, it will default to `pool_size`.
 |    padding: One of `"valid"` or `"same"` (case-insensitive).
 |      "valid" adds no padding.  "same" adds padding such that if the stride
 |      is 1, the output shape is the same as the input shape.
 |    data_format: A string,
 |      one of `channels_last` (default) or `channels_first`.
 |      The ordering of the dimensions in the inputs.
 |      `channels_last` corresponds to inputs with shape
 |      `(batch, steps, features)` while `channels_first`
 |      corresponds to inputs with shape
 |      `(batch, features, steps)`.
 |  
 |  Input shape:
 |    - If `data_format='channels_last'`:
 |      3D tensor with shape `(batch_size, steps, features)`.
 |    - If `data_format='channels_first'`:
 |      3D tensor with shape `(batch_size, features, steps)`.
 |  
 |  Output shape:
 |    - If `data_format='channels_last'`:
 |      3D tensor with shape `(batch_size, downsampled_steps, features)`.
 |    - If `data_format='channels_first'`:
 |      3D tensor with shape `(batch_size, features, downsampled_steps)`.

MaxPooling2D与MaxPooling2D/MaxPooling3D类似。

3.2.3 AveragePooling1D / 2D / 3D 3.2.4 Activation

此包下的激活函数的类型:relu/tanh/softmax/relu/linear/……

from tensorflow.python.keras.activations import deserialize
from tensorflow.python.keras.activations import elu
from tensorflow.python.keras.activations import exponential
from tensorflow.python.keras.activations import get
from tensorflow.python.keras.activations import hard_sigmoid
from tensorflow.python.keras.activations import linear
from tensorflow.python.keras.activations import relu
from tensorflow.python.keras.activations import selu
from tensorflow.python.keras.activations import serialize
from tensorflow.python.keras.activations import sigmoid
from tensorflow.python.keras.activations import softmax
from tensorflow.python.keras.activations import softplus
from tensorflow.python.keras.activations import softsign
from tensorflow.python.keras.activations import swish
from tensorflow.python.keras.activations import tanh

用法:

>>> layer = tf.keras.layers.Activation('relu')
>>> output = layer([-3.0, -1.0, 0.0, 2.0])
>>> list(output.numpy())
[0.0, 0.0, 0.0, 2.0]
>>> layer = tf.keras.layers.Activation(tf.nn.relu)
>>> output = layer([-3.0, -1.0, 0.0, 2.0])
>>> list(output.numpy())
[0.0, 0.0, 0.0, 2.0]

输入输出:

Input shape:
Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the batch axis) when using this layer as the first layer in a model.

Output shape:
Same shape as input.

3.2.5 Dense
keras.layers.core. Dense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs)
# Arguments
 |      units: Positive integer, dimensionality of the output space.
 |      activation: Activation function to use
 |          (see [activations](../activations.md)).
 |          If you don't specify anything, no activation is applied
 |          (ie. "linear" activation: `a(x) = x`).
 |      use_bias: Boolean, whether the layer uses a bias vector.
 |      kernel_initializer: Initializer for the `kernel` weights matrix
 |          (see [initializers](../initializers.md)).
 |      bias_initializer: Initializer for the bias vector
 |          (see [initializers](../initializers.md)).
 |      kernel_regularizer: Regularizer function applied to
 |          the `kernel` weights matrix
 |          (see [regularizer](../regularizers.md)).
 |      bias_regularizer: Regularizer function applied to the bias vector
 |          (see [regularizer](../regularizers.md)).
 |      activity_regularizer: Regularizer function applied to
 |          the output of the layer (its "activation").
 |          (see [regularizer](../regularizers.md)).
 |      kernel_constraint: Constraint function applied to
 |          the `kernel` weights matrix
 |          (see [constraints](../constraints.md)).
 |      bias_constraint: Constraint function applied to the bias vector
 |          (see [constraints](../constraints.md)).
 # Input shape  主义输入输出都是tensor
 |      nD tensor with shape: `(batch_size, ..., input_dim)`.
 |      The most common situation would be
 |      a 2D input with shape `(batch_size, input_dim)`.
 |  
 |  # Output shape
 |      nD tensor with shape: `(batch_size, ..., units)`.
 |      For instance, for a 2D input with shape `(batch_size, input_dim)`,
 |      the output would have shape `(batch_size, units)`.
 |  

官方的解释如下:
Just your regular densely-connected NN layer.
|
| Dense implements the operation:
| output = activation(dot(input, kernel) + bias)
| where activation is the element-wise activation function
| passed as the activation argument, kernel is a weights matrix
| created by the layer, and bias is a bias vector created by the layer
| (only applicable if use_bias is True).
|
| Note: if the input to the layer has a rank greater than 2, then
| it is flattened prior to the initial dot product with kernel.
|
| # Example
|
| ```python
| # as first layer in a sequential model:

| model = Sequential()
| model.add(Dense(32, input_shape=(16,)))
| # now the model will take as input arrays of shape (, 16)
| # and output arrays of shape (
, 32)
|
| # after the first layer, you don’t need to specify the size of the input anymore:
| model.add(Dense(32))
| ```

3.2.6 Dropout

为输入数据施加Dropout。Dropout将在训练过程中每次更新参数时随机断开一定百分比(p)的输入神经元连接,Dropout层用于防止过拟合。

class Dropout(keras.engine.base_layer.Layer)
 |  Dropout(rate, noise_shape=None, seed=None, **kwargs)
 |  
 |  Applies Dropout to the input.
 |  
 |  Dropout consists in randomly setting
 |  a fraction `rate` of input units to 0 at each update during training time,
 |  which helps prevent overfitting.
 |  
 |  # Arguments
 |      rate: float between 0 and 1. Fraction of the input units to drop.
 |      noise_shape: 1D integer tensor representing the shape of the
 |          binary dropout mask that will be multiplied with the input.
 |          For instance, if your inputs have shape
 |          `(batch_size, timesteps, features)` and
 |          you want the dropout mask to be the same for all timesteps,
 |          you can use `noise_shape=(batch_size, 1, features)`.
 |      seed: A Python integer to use as random seed.
 |  
 |  # References
 |      - [Dropout: A Simple Way to Prevent Neural Networks from Overfitting](
 |         http://www.jmlr.org/papers/volume15/srivastava14a/srivastava14a.pdf)
 

3.2.7 Embedding
class Embedding(keras.engine.base_layer.Layer)
     |  Embedding(input_dim, output_dim, embeddings_initializer='uniform', embeddings_regularizer=None, activity_regularizer=None, embeddings_constraint=None, mask_zero=False, input_length=None, **kwargs)
     |  
     |  Turns positive integers (indexes) into dense vectors of fixed size.
     |  eg. [[4], [20]] -> [[0.25, 0.1], [0.6, -0.2]]
     |  
     |  This layer can only be used as the first layer in a model.
     |  
     |  # Example
     |  
     |  ```python
     |    model = Sequential()
     |    model.add(Embedding(1000, 64, input_length=10))
     |    # the model will take as input an integer matrix of size (batch, input_length).
     |    # the largest integer (i.e. word index) in the input should be
     |    # no larger than 999 (vocabulary size).
     |    # now model.output_shape == (None, 10, 64), where None is the batch dimension.
     |  
     |    input_array = np.random.randint(1000, size=(32, 10))
     |  
     |    model.compile('rmsprop', 'mse')
     |    output_array = model.predict(input_array)
     |    assert output_array.shape == (32, 10, 64)
     |  ```
     |  
     |  # Arguments
     |      input_dim: int > 0. Size of the vocabulary,
     |          i.e. maximum integer index + 1.
     |      output_dim: int >= 0. Dimension of the dense embedding.
     |      embeddings_initializer: Initializer for the `embeddings` matrix
     |          (see [initializers](../initializers.md)).
     |      embeddings_regularizer: Regularizer function applied to
     |          the `embeddings` matrix
     |          (see [regularizer](../regularizers.md)).
     |      activity_regularizer: Regularizer function applied to
     |          the output of the layer (its "activation").
     |          (see [regularizer](../regularizers.md)).
     |      embeddings_constraint: Constraint function applied to
     |          the `embeddings` matrix
     |          (see [constraints](../constraints.md)).
     |      mask_zero: Whether or not the input value 0 is a special "padding"
     |          value that should be masked out.
     |          This is useful when using [recurrent layers](recurrent.md)
     |          which may take variable length input.
     |          If this is `True` then all subsequent layers
     |          in the model need to support masking or an exception will be raised.
     |          If mask_zero is set to True, as a consequence, index 0 cannot be
     |          used in the vocabulary (input_dim should equal size of
     |          vocabulary + 1).
     |      input_length: Length of input sequences, when it is constant.
     |          This argument is required if you are going to connect
     |          `Flatten` then `Dense` layers upstream
     |          (without it, the shape of the dense outputs cannot be computed).
     |  
     |  # Input shape
     |      2D tensor with shape: `(batch_size, sequence_length)`.
     |  
     |  # Output shape
     |      3D tensor with shape: `(batch_size, sequence_length, output_dim)`.
     |  
     |  # References
     |      - [A Theoretically Grounded Application of Dropout in
     |         Recurrent Neural Networks](http://arxiv.org/abs/1512.05287)
3.2.8 Flatten 3.2.9 SimpleRNN / LSTM / GRU 3.2.9.1 SimpleRNN
class SimpleRNN(RNN)
 |  SimpleRNN(units, activation='tanh', use_bias=True, kernel_initializer='glorot_uniform', recurrent_initializer='orthogonal', bias_initializer='zeros', kernel_regularizer=None, recurrent_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, recurrent_constraint=None, bias_constraint=None, dropout=0.0, recurrent_dropout=0.0, return_sequences=False, return_state=False, go_backwards=False, stateful=False, unroll=False, **kwargs)
 |  
 |  Fully-connected RNN where the output is to be fed back to input.
 |  
 |  # Arguments
 |      units: Positive integer, dimensionality of the output space.
 |      activation: Activation function to use
 |          (see [activations](../activations.md)).
 |          Default: hyperbolic tangent (`tanh`).
 |          If you pass `None`, no activation is applied
 |          (ie. "linear" activation: `a(x) = x`).
 |      use_bias: Boolean, whether the layer uses a bias vector.
 |      kernel_initializer: Initializer for the `kernel` weights matrix,
 |          used for the linear transformation of the inputs
 |          (see [initializers](../initializers.md)).
 |      recurrent_initializer: Initializer for the `recurrent_kernel`
 |          weights matrix,
 |          used for the linear transformation of the recurrent state
 |          (see [initializers](../initializers.md)).
 |      bias_initializer: Initializer for the bias vector
 |          (see [initializers](../initializers.md)).
 |      kernel_regularizer: Regularizer function applied to
 |          the `kernel` weights matrix
 |          (see [regularizer](../regularizers.md)).
 |      recurrent_regularizer: Regularizer function applied to
 |          the `recurrent_kernel` weights matrix
 |          (see [regularizer](../regularizers.md)).
 |      bias_regularizer: Regularizer function applied to the bias vector
 |          (see [regularizer](../regularizers.md)).
 |      activity_regularizer: Regularizer function applied to
 |          the output of the layer (its "activation").
 |          (see [regularizer](../regularizers.md)).
 |      kernel_constraint: Constraint function applied to
 |          the `kernel` weights matrix
 |          (see [constraints](../constraints.md)).
 |      recurrent_constraint: Constraint function applied to
 |          the `recurrent_kernel` weights matrix
 |          (see [constraints](../constraints.md)).
 |      bias_constraint: Constraint function applied to the bias vector
 |          (see [constraints](../constraints.md)).
 |      dropout: Float between 0 and 1.
 |          Fraction of the units to drop for
 |          the linear transformation of the inputs.
 |      recurrent_dropout: Float between 0 and 1.
 |          Fraction of the units to drop for
 |          the linear transformation of the recurrent state.
 |      return_sequences: Boolean. Whether to return the last output
 |          in the output sequence, or the full sequence.
 |      return_state: Boolean. Whether to return the last state
 |          in addition to the output.
 |      go_backwards: Boolean (default False).
 |          If True, process the input sequence backwards and return the
 |          reversed sequence.
 |      stateful: Boolean (default False). If True, the last state
 |          for each sample at index i in a batch will be used as initial
 |          state for the sample of index i in the following batch.
 |      unroll: Boolean (default False).
 |          If True, the network will be unrolled,
 |          else a symbolic loop will be used.
 |          Unrolling can speed-up a RNN,
 |          although it tends to be more memory-intensive.
 |          Unrolling is only suitable for short sequences.
 |  
3.2.9.2 LSTM
class LSTM(RNN)
 |  LSTM(units, activation='tanh', recurrent_activation='sigmoid', use_bias=True, kernel_initializer='glorot_uniform', recurrent_initializer='orthogonal', bias_initializer='zeros', unit_forget_bias=True, kernel_regularizer=None, recurrent_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, recurrent_constraint=None, bias_constraint=None, dropout=0.0, recurrent_dropout=0.0, implementation=2, return_sequences=False, return_state=False, go_backwards=False, stateful=False, unroll=False, **kwargs)
 |  
 |  Long Short-Term Memory layer - Hochreiter 1997.
 |  
 |  # Arguments
 |      units: Positive integer, dimensionality of the output space.
 |      activation: Activation function to use
 |          (see [activations](../activations.md)).
 |          Default: hyperbolic tangent (`tanh`).
 |          If you pass `None`, no activation is applied
 |          (ie. "linear" activation: `a(x) = x`).
 |      recurrent_activation: Activation function to use
 |          for the recurrent step
 |          (see [activations](../activations.md)).
 |          Default: sigmoid (`sigmoid`).
 |          If you pass `None`, no activation is applied
 |          (ie. "linear" activation: `a(x) = x`).
 |      use_bias: Boolean, whether the layer uses a bias vector.
 |      kernel_initializer: Initializer for the `kernel` weights matrix,
 |          used for the linear transformation of the inputs.
 |          (see [initializers](../initializers.md)).
 |      recurrent_initializer: Initializer for the `recurrent_kernel`
 |          weights matrix,
 |          used for the linear transformation of the recurrent state.
 |          (see [initializers](../initializers.md)).
 |      bias_initializer: Initializer for the bias vector
 |          (see [initializers](../initializers.md)).
 |      unit_forget_bias: Boolean.
 |          If True, add 1 to the bias of the forget gate at initialization.
 |          Setting it to true will also force `bias_initializer="zeros"`.
 |          This is recommended in [Jozefowicz et al. (2015)](
 |          http://www.jmlr.org/proceedings/papers/v37/jozefowicz15.pdf).
 |      kernel_regularizer: Regularizer function applied to
 |          the `kernel` weights matrix
 |          (see [regularizer](../regularizers.md)).
 |      recurrent_regularizer: Regularizer function applied to
 |          the `recurrent_kernel` weights matrix
 |          (see [regularizer](../regularizers.md)).
 |      bias_regularizer: Regularizer function applied to the bias vector
 |          (see [regularizer](../regularizers.md)).
 |      activity_regularizer: Regularizer function applied to
 |          the output of the layer (its "activation").
 |          (see [regularizer](../regularizers.md)).
 |      kernel_constraint: Constraint function applied to
 |          the `kernel` weights matrix
 |          (see [constraints](../constraints.md)).
 |      recurrent_constraint: Constraint function applied to
 |          the `recurrent_kernel` weights matrix
 |          (see [constraints](../constraints.md)).
 |      bias_constraint: Constraint function applied to the bias vector
 |          (see [constraints](../constraints.md)).
 |      dropout: Float between 0 and 1.
 |          Fraction of the units to drop for
 |          the linear transformation of the inputs.
 |      recurrent_dropout: Float between 0 and 1.
 |          Fraction of the units to drop for
 |          the linear transformation of the recurrent state.
 |      implementation: Implementation mode, either 1 or 2.
 |          Mode 1 will structure its operations as a larger number of
 |          smaller dot products and additions, whereas mode 2 will
 |          batch them into fewer, larger operations. These modes will
 |          have different performance profiles on different hardware and
 |          for different applications.
 |      return_sequences: Boolean. Whether to return the last output
 |          in the output sequence, or the full sequence.
 |      return_state: Boolean. Whether to return the last state
 |          in addition to the output. The returned elements of the
 |          states list are the hidden state and the cell state, respectively.
 |      go_backwards: Boolean (default False).
 |          If True, process the input sequence backwards and return the
 |          reversed sequence.
 |      stateful: Boolean (default False). If True, the last state
 |          for each sample at index i in a batch will be used as initial
 |          state for the sample of index i in the following batch.
 |      unroll: Boolean (default False).
 |          If True, the network will be unrolled,
 |          else a symbolic loop will be used.
 |          Unrolling can speed-up a RNN,
 |          although it tends to be more memory-intensive.
 |          Unrolling is only suitable for short sequences.
 |  
 |  # References
 |      - [Long short-term memory](
 |        http://www.bioinf.jku.at/publications/older/2604.pdf)
 |      - [Learning to forget: Continual prediction with LSTM](
 |        http://www.mitpressjournals.org/doi/pdf/10.1162/089976600300015015)
 |      - [Supervised sequence labeling with recurrent neural networks](
 |        http://www.cs.toronto.edu/~graves/preprint.pdf)
 |      - [A Theoretically Grounded Application of Dropout in
 |         Recurrent Neural Networks](https://arxiv.org/abs/1512.05287)
3.2.9.3 GRU
Help on class GRU in module keras.layers.recurrent:

class GRU(RNN)
 |  GRU(units, activation='tanh', recurrent_activation='sigmoid', use_bias=True, kernel_initializer='glorot_uniform', recurrent_initializer='orthogonal', bias_initializer='zeros', kernel_regularizer=None, recurrent_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, recurrent_constraint=None, bias_constraint=None, dropout=0.0, recurrent_dropout=0.0, implementation=2, return_sequences=False, return_state=False, go_backwards=False, stateful=False, unroll=False, reset_after=False, **kwargs)
 |  
 |  Gated Recurrent Unit - Cho et al. 2014.
 |  
 |  There are two variants. The default one is based on 1406.1078v3 and
 |  has reset gate applied to hidden state before matrix multiplication. The
 |  other one is based on original 1406.1078v1 and has the order reversed.
 |  
 |  The second variant is compatible with CuDNNGRU (GPU-only) and allows
 |  inference on CPU. Thus it has separate biases for `kernel` and
 |  `recurrent_kernel`. Use `'reset_after'=True` and
 |  `recurrent_activation='sigmoid'`.
 |  
 |  # Arguments
 |      units: Positive integer, dimensionality of the output space.
 |      activation: Activation function to use
 |          (see [activations](../activations.md)).
 |          Default: hyperbolic tangent (`tanh`).
 |          If you pass `None`, no activation is applied
 |          (ie. "linear" activation: `a(x) = x`).
 |      recurrent_activation: Activation function to use
 |          for the recurrent step
 |          (see [activations](../activations.md)).
 |          Default: sigmoid (`sigmoid`).
 |          If you pass `None`, no activation is applied
 |          (ie. "linear" activation: `a(x) = x`).
 |      use_bias: Boolean, whether the layer uses a bias vector.
 |      kernel_initializer: Initializer for the `kernel` weights matrix,
 |          used for the linear transformation of the inputs
 |          (see [initializers](../initializers.md)).
 |      recurrent_initializer: Initializer for the `recurrent_kernel`
 |          weights matrix,
 |          used for the linear transformation of the recurrent state
 |          (see [initializers](../initializers.md)).
 |      bias_initializer: Initializer for the bias vector
 |          (see [initializers](../initializers.md)).
 |      kernel_regularizer: Regularizer function applied to
 |          the `kernel` weights matrix
 |          (see [regularizer](../regularizers.md)).
 |      recurrent_regularizer: Regularizer function applied to
 |          the `recurrent_kernel` weights matrix
 |          (see [regularizer](../regularizers.md)).
 |      bias_regularizer: Regularizer function applied to the bias vector
 |          (see [regularizer](../regularizers.md)).
 |      activity_regularizer: Regularizer function applied to
 |          the output of the layer (its "activation").
 |          (see [regularizer](../regularizers.md)).
 |      kernel_constraint: Constraint function applied to
 |          the `kernel` weights matrix
 |          (see [constraints](../constraints.md)).
 |      recurrent_constraint: Constraint function applied to
 |          the `recurrent_kernel` weights matrix
 |          (see [constraints](../constraints.md)).
 |      bias_constraint: Constraint function applied to the bias vector
 |          (see [constraints](../constraints.md)).
 |      dropout: Float between 0 and 1.
 |          Fraction of the units to drop for
 |          the linear transformation of the inputs.
 |      recurrent_dropout: Float between 0 and 1.
 |          Fraction of the units to drop for
 |          the linear transformation of the recurrent state.
 |      implementation: Implementation mode, either 1 or 2.
 |          Mode 1 will structure its operations as a larger number of
 |          smaller dot products and additions, whereas mode 2 will
 |          batch them into fewer, larger operations. These modes will
 |          have different performance profiles on different hardware and
 |          for different applications.
 |      return_sequences: Boolean. Whether to return the last output
 |          in the output sequence, or the full sequence.
 |      return_state: Boolean. Whether to return the last state
 |          in addition to the output.
 |      go_backwards: Boolean (default False).
 |          If True, process the input sequence backwards and return the
 |          reversed sequence.
 |      stateful: Boolean (default False). If True, the last state
 |          for each sample at index i in a batch will be used as initial
 |          state for the sample of index i in the following batch.
 |      unroll: Boolean (default False).
 |          If True, the network will be unrolled,
 |          else a symbolic loop will be used.
 |          Unrolling can speed-up a RNN,
 |          although it tends to be more memory-intensive.
 |          Unrolling is only suitable for short sequences.
 |      reset_after: GRU convention (whether to apply reset gate after or
 |          before matrix multiplication). False = "before" (default),
 |          True = "after" (CuDNN compatible).
 |  
 |  # References
 |      - [Learning Phrase Representations using RNN Encoder-Decoder for
 |         Statistical Machine Translation](https://arxiv.org/abs/1406.1078)
 |      - [On the Properties of Neural Machine Translation:
 |         Encoder-Decoder Approaches](https://arxiv.org/abs/1409.1259)
 |      - [Empirical Evaluation of Gated Recurrent Neural Networks on
 |         Sequence Modeling](https://arxiv.org/abs/1412.3555v1)
 |      - [A Theoretically Grounded Application of Dropout in
 |         Recurrent Neural Networks](https://arxiv.org/abs/1512.05287)
3.3 estimator
Help on function model_to_estimator_v2 in module tensorflow.python.keras.estimator:

model_to_estimator_v2(keras_model=None, keras_model_path=None, custom_objects=None, model_dir=None, config=None, checkpoint_format='checkpoint', metric_names_map=None)
    Constructs an `Estimator` instance from given keras model.
    
    If you use infrastructure or other tooling that relies on Estimators, you can
    still build a Keras model and use model_to_estimator to convert the Keras
    model to an Estimator for use with downstream systems.
    
    For usage example, please see:
    [Creating estimators from Keras Models](
      https://www.tensorflow.org/guide/estimators#creating_estimators_from_keras_models).
    
    Sample Weights:
    Estimators returned by `model_to_estimator` are configured so that they can
    handle sample weights (similar to `keras_model.fit(x, y, sample_weights)`).
    
    To pass sample weights when training or evaluating the Estimator, the first
    item returned by the input function should be a dictionary with keys
    `features` and `sample_weights`. Example below:
    
    ```python
    keras_model = tf.keras.Model(...)
    keras_model.compile(...)
    
    estimator = tf.keras.estimator.model_to_estimator(keras_model)
    
    def input_fn():
      return dataset_ops.Dataset.from_tensors(
          ({'features': features, 'sample_weights': sample_weights},
           targets))
    
    estimator.train(input_fn, steps=1)
    ```
    
    Note: We do not support creating weighted metrics in Keras and converting them
    to weighted metrics in the Estimator API using `model_to_estimator`.
    You will have to create these metrics directly on the estimator spec using the
    `add_metrics` function.
    
    To customize the estimator `eval_metric_ops` names, you can pass in the
    `metric_names_map` dictionary mapping the keras model output metric names
    to the custom names as follows:
    
    ```python
      input_a = tf.keras.layers.Input(shape=(16,), name='input_a')
      input_b = tf.keras.layers.Input(shape=(16,), name='input_b')
      dense = tf.keras.layers.Dense(8, name='dense_1')
      interm_a = dense(input_a)
      interm_b = dense(input_b)
      merged = tf.keras.layers.concatenate([interm_a, interm_b], name='merge')
      output_a = tf.keras.layers.Dense(3, activation='softmax', name='dense_2')(
              merged)
      output_b = tf.keras.layers.Dense(2, activation='softmax', name='dense_3')(
              merged)
      keras_model = tf.keras.models.Model(
          inputs=[input_a, input_b], outputs=[output_a, output_b])
      keras_model.compile(
          loss='categorical_crossentropy',
          optimizer='rmsprop',
          metrics={
              'dense_2': 'categorical_accuracy',
              'dense_3': 'categorical_accuracy'
          })
    
      metric_names_map = {
          'dense_2_categorical_accuracy': 'acc_1',
          'dense_3_categorical_accuracy': 'acc_2',
      }
      keras_est = tf.keras.estimator.model_to_estimator(
          keras_model=keras_model,
          config=config,
          metric_names_map=metric_names_map)
    ```
    
    Args:
      keras_model: A compiled Keras model object. This argument is mutually
        exclusive with `keras_model_path`. Estimator's `model_fn` uses the
        structure of the model to clone the model. Defaults to `None`.
      keras_model_path: Path to a compiled Keras model saved on disk, in HDF5
        format, which can be generated with the `save()` method of a Keras model.
        This argument is mutually exclusive with `keras_model`.
        Defaults to `None`.
      custom_objects: Dictionary for cloning customized objects. This is
        used with classes that is not part of this pip package. For example, if
        user maintains a `relu6` class that inherits from `tf.keras.layers.Layer`,
        then pass `custom_objects={'relu6': relu6}`. Defaults to `None`.
      model_dir: Directory to save `Estimator` model parameters, graph, summary
        files for TensorBoard, etc. If unset a directory will be created with
        `tempfile.mkdtemp`
      config: `RunConfig` to config `Estimator`. Allows setting up things in
        `model_fn` based on configuration such as `num_ps_replicas`, or
        `model_dir`. Defaults to `None`. If both `config.model_dir` and the
        `model_dir` argument (above) are specified the `model_dir` **argument**
        takes precedence.
      checkpoint_format: Sets the format of the checkpoint saved by the estimator
        when training. May be `saver` or `checkpoint`, depending on whether to
        save checkpoints from `tf.compat.v1.train.Saver` or `tf.train.Checkpoint`.
        The default is `checkpoint`. Estimators use name-based `tf.train.Saver`
        checkpoints, while Keras models use object-based checkpoints from
        `tf.train.Checkpoint`. Currently, saving object-based checkpoints from
        `model_to_estimator` is only supported by Functional and Sequential
        models. Defaults to 'checkpoint'.
      metric_names_map: Optional dictionary mapping Keras model output metric
        names to custom names. This can be used to override the default Keras
        model output metrics names in a multi IO model use case and provide custom
        names for the `eval_metric_ops` in Estimator.
        The Keras model metric names can be obtained using `model.metrics_names`
        excluding any loss metrics such as total loss and output losses.
        For example, if your Keras model has two outputs `out_1` and `out_2`,
        with `mse` loss and `acc` metric, then `model.metrics_names` will be
        `['loss', 'out_1_loss', 'out_2_loss', 'out_1_acc', 'out_2_acc']`.
        The model metric names excluding the loss metrics will be
        `['out_1_acc', 'out_2_acc']`.
    
    Returns:
      An Estimator from given keras model.
    
    Raises:
      ValueError: If neither keras_model nor keras_model_path was given.
      ValueError: If both keras_model and keras_model_path was given.
      ValueError: If the keras_model_path is a GCS URI.
      ValueError: If keras_model has not been compiled.
      ValueError: If an invalid checkpoint_format was given.
3.4 losses

下面列出了都有哪些损失函数可用:比如mse/mae/categorical_crossentropy/binary_crossentropy/msle等

from tensorflow.python.keras.losses import BinaryCrossentropy
from tensorflow.python.keras.losses import CategoricalCrossentropy
from tensorflow.python.keras.losses import CategoricalHinge
from tensorflow.python.keras.losses import CosineSimilarity
from tensorflow.python.keras.losses import Hinge
from tensorflow.python.keras.losses import Huber
from tensorflow.python.keras.losses import KLDivergence
from tensorflow.python.keras.losses import LogCosh
from tensorflow.python.keras.losses import Loss
from tensorflow.python.keras.losses import MeanAbsoluteError
from tensorflow.python.keras.losses import MeanAbsolutePercentageError
from tensorflow.python.keras.losses import MeanSquaredError
from tensorflow.python.keras.losses import MeanSquaredLogarithmicError
from tensorflow.python.keras.losses import Poisson
from tensorflow.python.keras.losses import SparseCategoricalCrossentropy
from tensorflow.python.keras.losses import SquaredHinge
from tensorflow.python.keras.losses import binary_crossentropy
from tensorflow.python.keras.losses import categorical_crossentropy
from tensorflow.python.keras.losses import categorical_hinge
from tensorflow.python.keras.losses import cosine_similarity
from tensorflow.python.keras.losses import deserialize
from tensorflow.python.keras.losses import get
from tensorflow.python.keras.losses import hinge
from tensorflow.python.keras.losses import huber
from tensorflow.python.keras.losses import kl_divergence
from tensorflow.python.keras.losses import kl_divergence as KLD
from tensorflow.python.keras.losses import kl_divergence as kld
from tensorflow.python.keras.losses import kl_divergence as kullback_leibler_divergence
from tensorflow.python.keras.losses import log_cosh
from tensorflow.python.keras.losses import log_cosh as logcosh
from tensorflow.python.keras.losses import mean_absolute_error
from tensorflow.python.keras.losses import mean_absolute_error as MAE
from tensorflow.python.keras.losses import mean_absolute_error as mae
from tensorflow.python.keras.losses import mean_absolute_percentage_error
from tensorflow.python.keras.losses import mean_absolute_percentage_error as MAPE
from tensorflow.python.keras.losses import mean_absolute_percentage_error as mape
from tensorflow.python.keras.losses import mean_squared_error
from tensorflow.python.keras.losses import mean_squared_error as MSE
from tensorflow.python.keras.losses import mean_squared_error as mse
from tensorflow.python.keras.losses import mean_squared_logarithmic_error
from tensorflow.python.keras.losses import mean_squared_logarithmic_error as MSLE
from tensorflow.python.keras.losses import mean_squared_logarithmic_error as msle
from tensorflow.python.keras.losses import poisson
from tensorflow.python.keras.losses import serialize
from tensorflow.python.keras.losses import sparse_categorical_crossentropy
from tensorflow.python.keras.losses import squared_hinge
from tensorflow.python.ops.losses.loss_reduction import ReductionV2 as Reduction
3.5 optimizers

下面列出了都有哪些优化器可用:比如SGD/RMSprop/Adam等

from . import schedules
from tensorflow.python.keras.optimizer_v2.adadelta import Adadelta
from tensorflow.python.keras.optimizer_v2.adagrad import Adagrad
from tensorflow.python.keras.optimizer_v2.adam import Adam
from tensorflow.python.keras.optimizer_v2.adamax import Adamax
from tensorflow.python.keras.optimizer_v2.ftrl import Ftrl
from tensorflow.python.keras.optimizer_v2.gradient_descent import SGD
from tensorflow.python.keras.optimizer_v2.nadam import Nadam
from tensorflow.python.keras.optimizer_v2.optimizer_v2 import OptimizerV2 as Optimizer
from tensorflow.python.keras.optimizer_v2.rmsprop import RMSprop
from tensorflow.python.keras.optimizers import deserialize
from tensorflow.python.keras.optimizers import get
from tensorflow.python.keras.optimizers import serialize
3.6 metrics

下面列出了都有哪些评价指标可用:比如:AUC/Accuracy/Categorical_accuracy/等

from tensorflow.python.keras.losses import binary_crossentropy
from tensorflow.python.keras.losses import categorical_crossentropy
from tensorflow.python.keras.losses import hinge
from tensorflow.python.keras.losses import kl_divergence
from tensorflow.python.keras.losses import kl_divergence as KLD
from tensorflow.python.keras.losses import kl_divergence as kld
from tensorflow.python.keras.losses import kl_divergence as kullback_leibler_divergence
from tensorflow.python.keras.losses import mean_absolute_error
from tensorflow.python.keras.losses import mean_absolute_error as MAE
from tensorflow.python.keras.losses import mean_absolute_error as mae
from tensorflow.python.keras.losses import mean_absolute_percentage_error
from tensorflow.python.keras.losses import mean_absolute_percentage_error as MAPE
from tensorflow.python.keras.losses import mean_absolute_percentage_error as mape
from tensorflow.python.keras.losses import mean_squared_error
from tensorflow.python.keras.losses import mean_squared_error as MSE
from tensorflow.python.keras.losses import mean_squared_error as mse
from tensorflow.python.keras.losses import mean_squared_logarithmic_error
from tensorflow.python.keras.losses import mean_squared_logarithmic_error as MSLE
from tensorflow.python.keras.losses import mean_squared_logarithmic_error as msle
from tensorflow.python.keras.losses import poisson
from tensorflow.python.keras.losses import sparse_categorical_crossentropy
from tensorflow.python.keras.losses import squared_hinge
from tensorflow.python.keras.metrics import AUC
from tensorflow.python.keras.metrics import Accuracy
from tensorflow.python.keras.metrics import BinaryAccuracy
from tensorflow.python.keras.metrics import BinaryCrossentropy
from tensorflow.python.keras.metrics import CategoricalAccuracy
from tensorflow.python.keras.metrics import CategoricalCrossentropy
from tensorflow.python.keras.metrics import CategoricalHinge
from tensorflow.python.keras.metrics import CosineSimilarity
from tensorflow.python.keras.metrics import FalseNegatives
from tensorflow.python.keras.metrics import FalsePositives
from tensorflow.python.keras.metrics import Hinge
from tensorflow.python.keras.metrics import KLDivergence
from tensorflow.python.keras.metrics import LogCoshError
from tensorflow.python.keras.metrics import Mean
from tensorflow.python.keras.metrics import MeanAbsoluteError
from tensorflow.python.keras.metrics import MeanAbsolutePercentageError
from tensorflow.python.keras.metrics import MeanIoU
from tensorflow.python.keras.metrics import MeanRelativeError
from tensorflow.python.keras.metrics import MeanSquaredError
from tensorflow.python.keras.metrics import MeanSquaredLogarithmicError
from tensorflow.python.keras.metrics import MeanTensor
from tensorflow.python.keras.metrics import Metric
from tensorflow.python.keras.metrics import Poisson
from tensorflow.python.keras.metrics import Precision
from tensorflow.python.keras.metrics import PrecisionAtRecall
from tensorflow.python.keras.metrics import Recall
from tensorflow.python.keras.metrics import RecallAtPrecision
from tensorflow.python.keras.metrics import RootMeanSquaredError
from tensorflow.python.keras.metrics import SensitivityAtSpecificity
from tensorflow.python.keras.metrics import SparseCategoricalAccuracy
from tensorflow.python.keras.metrics import SparseCategoricalCrossentropy
from tensorflow.python.keras.metrics import SparseTopKCategoricalAccuracy
from tensorflow.python.keras.metrics import SpecificityAtSensitivity
from tensorflow.python.keras.metrics import SquaredHinge
from tensorflow.python.keras.metrics import Sum
from tensorflow.python.keras.metrics import TopKCategoricalAccuracy
from tensorflow.python.keras.metrics import TrueNegatives
from tensorflow.python.keras.metrics import TruePositives
from tensorflow.python.keras.metrics import binary_accuracy
from tensorflow.python.keras.metrics import categorical_accuracy
from tensorflow.python.keras.metrics import deserialize
from tensorflow.python.keras.metrics import get
from tensorflow.python.keras.metrics import serialize
from tensorflow.python.keras.metrics import sparse_categorical_accuracy
from tensorflow.python.keras.metrics import sparse_top_k_categorical_accuracy
from tensorflow.python.keras.metrics import top_k_categorical_accuracy

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/715240.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存