文档The Sequential model 已于 2020/04/12 更新。 Specifying the input shape in advance 部分澄清了这个问题。
当您实例化一个没有输入形状的 Sequential 模型时,它不是“构建”的:它没有权重(调用 model.weights 会导致错误说明这一点)。当模型首先看到一些输入数据时创建权重:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
model1 = keras.Sequential(
[
layers.Dense(2, activation="relu"),
layers.Dense(3, activation="relu"),
layers.Dense(4),
]
) # No weights at this stage!
for layer in model1.layers:
print(layer.weights) # Empty
# At this point, you can't do this:
# model1.weights
# You also can't do this:
# model1.summary()
# Call the model on a test input
x = tf.ones((1, 4))
y = model1(x)
# Once a model is "built", you can call its summary() method to display its contents:
model1.summary()
您可以通过将Input 对象传递给您的模型来启动您的模型,以便它从一开始就知道其输入形状:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
model2 = keras.Sequential()
model2.add(keras.Input(shape=(4,)))
model2.add(layers.Dense(2, activation="relu"))
model2.add(layers.Dense(3, activation="relu"))
model2.add(layers.Dense(4))
for layer in model2.layers:
print(layer.weights)
print(model2.weights)
model2.summary()
一个简单的替代方法是将input_shape 参数传递给您的第一层:
model2.add(layers.Dense(2, activation="relu", input_shape=(4,)))
使用此类预定义输入形状构建的模型始终具有权重(甚至在看到任何数据之前),并且始终具有定义的输出形状。
最后,文件说:
一般来说,如果您知道顺序模型的输入形状是什么,建议您始终提前指定输入形状。
回到你的问题:
它如何知道预期的形状?
如果没有定义输入形状,模型将匹配它首先看到的数据。
x = tf.ones((1, 4))
y = model1(x)
model1.summary()
# Output
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_2 (Dense) (1, 2) 10
_________________________________________________________________
dense_3 (Dense) (1, 3) 9
_________________________________________________________________
dense_4 (Dense) (1, 4) 16
=================================================================
Total params: 35
Trainable params: 35
Non-trainable params: 0
_________________________________________________________________
x = tf.ones((3, 5, 10))
y = model1(x)
model1.summary()
# Output:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_2 (Dense) (3, 5, 2) 22
_________________________________________________________________
dense_3 (Dense) (3, 5, 3) 9
_________________________________________________________________
dense_4 (Dense) (3, 5, 4) 16
=================================================================
Total params: 47
Trainable params: 47
Non-trainable params: 0
_________________________________________________________________
如果我不提供输入形状,默认行为是什么?它将如何影响我的模型?
如果你没有预先指定模型的输入形状,模型没有权重,你不能调用model.summary(),因为它没有被构建。