I have a simple network:
input_layer = Input(1)
inner_layer = Dense(4, activation='relu')(input_layer)
output_layer = Dense(1, activation='linear')(inner_layer)
model = Model(input_layer, output_layer)
optimizer = Adam(learning_rate=0.01)
model.compile(optimizer=optimizer, loss='mse')
Intuitively, inference for input 0
would simply be model.predict(0)
. However this generates this error: expected input_2 to have 2 dimensions, but got array with shape ()
I understand it expects the input (which is a single number) to be two dimensional, but I don't understand what Tensorflow accepts as valid input. I tried many different combinations of inputs, some work and some don't, it seems quite inconsistent and the warnings/error are usually not useful:
When calling model.predict()
:
model.predict(0)
- Throwsmodel.predict([0])
- Worksmodel.predict([[0]])
- Works
When calling model()
(I saw here that's needed to get the gradients):
model(0)
- Throwsmodel([0])
- Throwsmodel([[0]])
- Throws
When using np.reshape
:
model(np.reshape(0,[1,1]))
- Worksmodel(np.reshape([0],[1,1]))
- Worksmodel(np.reshape([[0]],[1,1]))
- Works
What seems to be working consistently is using numpy's reshape
function. it always works both for model.predict()
and for model()
on all the inputs as long as they're reshaped to a [1,1]
shape.
My questions:
- What are the guidelines to feeding inputs into tensorflow models in regards to inputs shapes/types?
- What does "
shape ()
" mean? - What does "
(None, 1)
" mean? - Why does
reshape
work but[[0]]
does not? Both create a 2-dimensional collection. - Why when calling
model(0)
/model([0])
/model([[0]])
does this warning show:WARNING:tensorflow:Model was constructed with shape Tensor("input_1:0", shape=(None, 1), dtype=float32) for input (None, 1), but it was re-called on a Tensor with incompatible shape ()
?