Keras Sequential,不提供输入形状 [英] Keras Sequential without providing input shape
问题描述
我目前有一个像这样的keras模型:
I currently have a keras model that looks like this:
model = keras.Sequential()
model.add(keras.layers.Dense(100, activation=tf.nn.relu))
model.add(keras.layers.Dense(100, activation=tf.nn.relu))
model.add(keras.layers.Dense(len(labels), activation=tf.nn.softmax))
Keras文档告诉我:
模型需要知道它应该期望什么样的输入形状.因此,顺序模型中的第一层(并且只有第一层,因为随后的层可以进行自动形状推断)需要接收有关其输入形状的信息
The model needs to know what input shape it should expect. For this reason, the first layer in a Sequential model (and only the first, because following layers can do automatic shape inference) needs to receive information about its input shape
但是,即使我从未指定输入的形状,该模型实际上也可以正常运行,没有错误.
However, the model as it is actually trains fine, without errors, even though I never specified the shape of the inputs.
它如何知道预期的形状?如果我不提供输入形状,默认行为是什么?它将如何影响我的模型?
How does it know what shape to expect? What is the default behaviour if I don't provide an input shape? How will it affect my model?
这是使用tf.keras
,也就是keras的Tensorflow后端
edit: this is using tf.keras
, aka the Tensorflow backend for keras
推荐答案
很好的观察-我认为应该更新Keras文档.如果未提供输入形状,Keras将从Model.fit
的参数x
推断出它,然后才构建整个模型.具体来说,这就是正在发生的事情:
Nice observation - I believe the Keras documentation should be updated. When the input shape is not provided, Keras infers it from the argument x
of Model.fit
and only then it builds the whole model. Concretely, this is what's happening:
- 在
Sequential
模型中添加Keras图层时,由于从未设置参数input_shape
(并且扩展名为batch_input_shape
),因此属性Model.inputs
仍为None
(请参见Model.fit
和Model._set_inputs
).
- When adding Keras layers in the
Sequential
model, since the argumentinput_shape
(and, by extension,batch_input_shape
) is never set, the attributeModel.inputs
remainsNone
(seeSequential.add
). - Then, in
Model.fit
, they check whetherModel.inputs
has been set (seeModel.fit
andModel._standardize_user_data
) and, when it hasn't, they infer the input shape from the provided input array. - Finally, in
Model._set_inputs
, they build the whole model with the inferredinput_shape
(seeModel._set_inputs
).
这可以通过在拟合模型之前打印一些砝码(例如print(model.layers[0].get_weights())
)来验证.您会看到,当没有在模型的第一层提供参数input_shape
或batch_input_shape
时,权重数组为空,因为尚未建立模型.
This can be verified by printing some weights (e.g. print(model.layers[0].get_weights())
) before fitting the model. You will see that, when the argument input_shape
or batch_input_shape
is not provided to the first layer of the model, the weight's array is empty as the model is yet to be built.
这篇关于Keras Sequential,不提供输入形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!