當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。