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(...)
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值