Tensorflow中Sequential model

sequential模型适用于简单堆叠网络层(a plain stack of layers),及每一层只有一个输入和一个输出:

几种创建sequential模型的方法:


# 1.
model = keras.Sequential(
    [
        layers.Dense(2, activation="relu", name="layer1"),
        layers.Dense(3, activation="relu", name="layer2"),
        layers.Dense(4, name="layer3"),
    ]
)
x = tf.ones((3, 3))#第一个3理解为batch
y = model(x)
# 2.
layer1 = layers.Dense(2, activation="relu", name="layer1")
layer2 = layers.Dense(3, activation="relu", name="layer2")
layer3 = layers.Dense(4, name="layer3")
# 3.
model = keras.Sequential(name="my_sequential")
model.add(layers.Dense(2, activation="relu", name="layer1"))
model.add(layers.Dense(3, activation="relu", name="layer2"))
model.add(layers.Dense(4, name="layer3"))

通常想要查看sequential模型,必须给模型传入一个输入例如:

model = keras.Sequential(
    [
        layers.Dense(2, activation="relu"),
        layers.Dense(3, activation="relu"),
        layers.Dense(4),
    ]
) 
# 此时输入model.summary(),model.weights都会报错
# 必须给模型一个输入参数
x = tf.ones((1, 4))  #参数1理解为batch
y = model(x)
print("Number of weights after calling the model:", len(model.weights))  # 6
(改进)可以在创建model时,就传入输入:
model = keras.Sequential()
model.add(keras.Input(shape=(4,)))#注意此时的输入的shape和上面情况对比
model.add(layers.Dense(2, activation="relu"))
model.add(layers.Dense(3, activation="relu"))
model.add(layers.Dense(4,))
(改进):
model = keras.Sequential()
model.add(layers.Dense(2, activation="relu", input_shape=(4,)))
model.add(layers.Dense(3, activation="relu"))
model.add(layers.Dense(4,))

提取sequential创建网络的某一层:

initial_model = keras.Sequential(
    [
        keras.Input(shape=(250, 250, 3)),
        layers.Conv2D(32, 5, strides=2, activation="relu"),
        layers.Conv2D(32, 3, activation="relu", name="my_intermediate_layer"),
        layers.Conv2D(32, 3, activation="relu"),
    ]
)
feature_extractor = keras.Model(inputs=initial_model.inputs,
 outputs=initial_model.get_layer(name="my_intermediate_layer").output,
x = tf.ones((1, 250, 250, 3))
features = feature_extractor(x)

sequential转移学习框架

model = keras.Sequential([
    keras.Input(shape=(784))
    layers.Dense(32, activation='relu'),
    layers.Dense(10),
])
#模型加载已训练好的参数
model.load_weights(...)
#除了最后一层 其他层都不用训练
for layer in model.layers[:-1]:
  layer.trainable = False
model.compile(...)
model.fit(...)
上一篇:keras_3_Model 类 API


下一篇:使用注意力机制建模 - 标准化日期格式