Keras Sequential without providing input shape

你说的曾经没有我的故事 提交于 2020-12-30 07:00:06

问题


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))

The Keras documentation tells me:

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?

edit: this is using tf.keras, aka the Tensorflow backend for keras


回答1:


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. 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).

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.




回答2:


There appears to be a miscorrelation between the TensorFlow version and the Keras documentation.

As far as I can see, the documentation that you are referring to is the one from Keras, while the TensorFlow version that you use is 2.0.0 >= alpha version, whose Keras internal version is different.

You should check the Keras documentation within TensorFlow 2.0, otherwise you might very well run into mismatches.



来源:https://stackoverflow.com/questions/57871463/keras-sequential-without-providing-input-shape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!