Reputation: 150
When I define a model like:
import tensorflow as tf
from tensorflow.keras import layers
import numpy as np
input_shape = (20,20)
input = tf.keras.Input(shape=input_shape)
nn = layers.Flatten()(input)
nn = layers.Dense(10)(nn)
output = layers.Activation('sigmoid')(nn)
model = tf.keras.Model(inputs=input, outputs=output)
Why do I need to add another dimension to my actual input:
actual_input = np.ones((1,20,20))
prediction = model.predict(actual_input)
why can't I just do actual_input = np.ones((20,20))
?
Edit:
in the docs it says something about batchsize.. Is this batchsize somehow related to my question? If so, why would I need it, when I want to predict with my model? Thanks for any help.
Upvotes: 0
Views: 128
Reputation: 15023
In Keras
(TensorFlow
), one cannot predict on a single input. Therefore, even if you have a single example, you need to add the batch_axis
to it.
Practically, in this situation, you have a batch size of 1, hence the batch axis.
This is how TensorFlow
and Keras
are built, and even for a single prediction you need to add the batch axis (batch size of 1 == 1 single example).
You can use np.expand_dims(input,axis=0)
or tf.expand_dims(input,axis=0)
to transform your input into a suitable format for prediction.
Upvotes: 1