当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python tf.keras.Sequential用法及代码示例


Sequential 将层的线性堆栈分组为 tf.keras.Model

继承自:ModelLayerModule

用法

tf.keras.Sequential(
    layers=None, name=None
)

参数

  • layers 要添加到模型的可选层列表。
  • name 模型的可选名称。

属性

  • distribute_strategy 该模型是在 tf.distribute.Strategy 下创建的。
  • layers
  • metrics_names 返回所有输出的模型显示标签。

    注意:metrics_names 仅在 keras.Model 已根据实际数据进行训练/评估后可用。

    inputs = tf.keras.layers.Input(shape=(3,))
    outputs = tf.keras.layers.Dense(2)(inputs)
    model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
    model.compile(optimizer="Adam", loss="mse", metrics=["mae"])
    model.metrics_names
    []
    x = np.random.random((2, 3))
    y = np.random.randint(0, 2, (2, 2))
    model.fit(x, y)
    model.metrics_names
    ['loss', 'mae']
    inputs = tf.keras.layers.Input(shape=(3,))
    d = tf.keras.layers.Dense(2, name='out')
    output_1 = d(inputs)
    output_2 = d(inputs)
    model = tf.keras.models.Model(
       inputs=inputs, outputs=[output_1, output_2])
    model.compile(optimizer="Adam", loss="mse", metrics=["mae", "acc"])
    model.fit(x, (y, y))
    model.metrics_names
    ['loss', 'out_loss', 'out_1_loss', 'out_mae', 'out_acc', 'out_1_mae',
    'out_1_acc']
  • run_eagerly 指示模型是否应立即运行的可设置属性。

    即刻地运行意味着您的模型将像 Python 代码一样逐步运行。您的模型可能会运行得更慢,但通过单步调用各个层调用,您应该可以更轻松地对其进行调试。

    默认情况下,我们将尝试将您的模型编译为静态图以提供最佳执行性能。

Sequential 在此模型上提供训练和推理函数。

例子:

# Optionally, the first layer can receive an `input_shape` argument:
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(8, input_shape=(16,)))
# Afterwards, we do automatic shape inference:
model.add(tf.keras.layers.Dense(4))

# This is identical to the following:
model = tf.keras.Sequential()
model.add(tf.keras.Input(shape=(16,)))
model.add(tf.keras.layers.Dense(8))

# Note that you can also omit the `input_shape` argument.
# In that case the model doesn't have any weights until the first call
# to a training/evaluation method (since it isn't yet built):
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(8))
model.add(tf.keras.layers.Dense(4))
# model.weights not created yet

# Whereas if you specify the input shape, the model gets built
# continuously as you are adding layers:
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(8, input_shape=(16,)))
model.add(tf.keras.layers.Dense(4))
len(model.weights)
# Returns "4"

# When using the delayed-build pattern (no input shape specified), you can
# choose to manually build your model by calling
# `build(batch_input_shape)`:
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(8))
model.add(tf.keras.layers.Dense(4))
model.build((None, 16))
len(model.weights)
# Returns "4"

# Note that when using the delayed-build pattern (no input shape specified),
# the model gets built the first time you call `fit`, `eval`, or `predict`,
# or the first time you call the model on some input data.
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(8))
model.add(tf.keras.layers.Dense(1))
model.compile(optimizer='sgd', loss='mse')
# This builds the model for the first time:
model.fit(x, y, batch_size=32, epochs=10)

相关用法


注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.keras.Sequential。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。