Keras Sequential,不提供输入形状 [英] Keras Sequential without providing input shape

查看:70
本文介绍了Keras Sequential,不提供输入形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个像这样的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:

  1. Sequential模型中添加Keras图层时,由于从未设置参数input_shape(并且扩展名为batch_input_shape),因此属性Model.inputs仍为None(请参见
  1. When adding Keras layers in the Sequential model, since the argument input_shape (and, by extension, batch_input_shape) is never set, the attribute Model.inputs remains None (see Sequential.add).
  2. Then, in Model.fit, they check whether Model.inputs has been set (see Model.fit and Model._standardize_user_data) and, when it hasn't, they infer the input shape from the provided input array.
  3. Finally, in Model._set_inputs, they build the whole model with the inferred input_shape (see Model._set_inputs).


这可以通过在拟合模型之前打印一些砝码(例如print(model.layers[0].get_weights()))来验证.您会看到,当没有在模型的第一层提供参数input_shapebatch_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆