Neural Network Input for predictions does not match

Excuse my bad writing english is not my first language. I hope someone knows the answer to this. I am quite new to Keras.

So I have a problem with a Keras Neural Network in Python, I created my Sequential Network and have it learn with data from a file. Then I saved the Network as a .keras file and loaded it in another script with “model = load_model(‘alphabet_recognition_model.keras’, compile=False)” my
layout looks like this “model = keras.Sequential([
layers.Dense(128, activation=keras.layers.LeakyReLU(alpha=0.01), input_shape=(63,))
] + [layers.Dense(128, activation=keras.layers.LeakyReLU(alpha=0.01))] * 3 + [
layers.Dense(26, activation=‘softmax’)
])”. My problem arises when I try to use “model.predict(x)”, if I do that it tells me my input shape is incorrect.

Like this: ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 63 but received input with shape (None, 1)

This is my array I want to input: [ 606 563 0 -71 -34 -7 -108 -112 -6 -120 -176 -11 -142 -216
-11 -81 -176 21 -82 -246 -15 -79 -188 -32 -72 -146 -34 -42
-176 11 -41 -247 -32 -44 -171 -40 -38 -130 -30 -2 -168 -5
-4 -226 -46 -11 -151 -31 -7 -111 -5 39 -153 -22 32 -205
-41 21 -155 -24 21 -121 -2]

This is a Numpy Array with length 63 and shape (63,)

Am I just stupid or is my Neural Network broken?

Hi @KompetenzBot, Before passing x to model.predict could you please add bath dimension by

tf.expand_dims(x, axis=0)

and pass it to model.predict. Thank You.

1 Like

I tried that it doesn’t work maybe me loading the model from a .keras file might be the problem.
model = load_model(‘alphabet_recognition_model.keras’, compile=False)
model.summary()
tf.expand_dims(x, axis=0)
prediction = model.predict(x)

This is how I do it.

Hi @KompetenzBot, You have to assign tf.expand_dims to a variable and have to pass that variable to the model.predict(). For example

model = load_model(‘alphabet_recognition_model.keras’, compile=False)
model.summary()
add_dim_x=tf.expand_dims(x, axis=0)
prediction = model.predict(add_dim_x)

Thank You.

1 Like

Thank you sooooo much it finally works. I appreciate your help. <3 Thanks again.