Issue
I'm successfully loading a model from a file, and when I try to predict output on any input with shape (480,) I'm getting ValueError as follows:
ValueError: Input 0 of layer sequential_1 is incompatible with the layer: expected axis -1 of input shape to have value 480 but received input with shape (32, 1)
The code that I'm using
import numpy as np
from keras.models import load_model
model = load_model("my_model.h5")
sample = np.zeros((480,))
model.predict(sample)
Do you have any idea why is this happening?
My model summary after loading:
Training of this model looks reasonable, but if I'm trying to predict values just after invoking model.compile() the same error happens.
model = keras.models.Sequential([
keras.layers.Input(shape=(480,)),
keras.layers.Dense(1024, activation="relu", name="layer1"),
keras.layers.Dense(1024, activation="relu", name="layer2"),
keras.layers.Dense(1024, activation="relu", name="layer3"),
keras.layers.Dense(1024, activation="relu", name="layer4"),
keras.layers.Dense(38, activation="softmax", name="output")
])
model.compile(
optimizer=keras.optimizers.Adam(lr=0.0003),
loss='categorical_crossentropy',
metrics=['categorical_accuracy']
)
sample = np.zeros((480,))
model.predict(sample) # ValueError
model.fit(...) # works fine
Solution
This works for me instead; I added "1" to the shape of the input sample:
import numpy as np
model = keras.models.Sequential([
keras.layers.Input(shape=(480,)),
keras.layers.Dense(1024, activation="relu", name="layer1"),
keras.layers.Dense(1024, activation="relu", name="layer2"),
keras.layers.Dense(1024, activation="relu", name="layer3"),
keras.layers.Dense(1024, activation="relu", name="layer4"),
keras.layers.Dense(38, activation="softmax", name="output")
])
model.compile(
optimizer=keras.optimizers.Adam(lr=0.0003),
loss='categorical_crossentropy',
metrics=['categorical_accuracy']
)
sample = np.zeros((1, 480,))
model.predict(sample) # No ValueError
Answered By - J369
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.