I am building a keras model for a multi-class classification problem. my data set has 7 numerical features and 4 labels. I have structured the model as follows:
def create_keras_model():
initializer = tf.keras.initializers.GlorotNormal()
return tf.keras.models.Sequential([
#tf.keras.layers.Input(shape=(7,)),
LSTM(units=20,kernel_initializer = initializer
input_shape=(7,)),
tf.keras.layers.Dense(4,),
tf.keras.layers.Softmax(),
])
and when I compile it, got this error:
ValueError: Input 0 of layer "lstm" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 7)
What could be the problem here? and how can I fix it?
CodePudding user response:
You need to change intput_shape
and use tf.expand_dims()
on X to add one dimension at the end, then you can use your model and start training.
tf.keras.layers.LSTM(units=20, input_shape=(7,1))
x = tf.expand_dims(x, axis=-1)
Full code:
import tensorflow as tf
def create_keras_model():
initializer = tf.keras.initializers.GlorotNormal()
return tf.keras.models.Sequential([
tf.keras.layers.LSTM(units=20,kernel_initializer = initializer,
input_shape=(7,1)),
tf.keras.layers.Dense(4,),
tf.keras.layers.Softmax(),
])
x = tf.random.normal((100,7))
y = tf.random.normal((100,4))
x = tf.expand_dims(x, axis=-1)
model = create_keras_model()
model.compile(optimizer='adam', loss = 'categorical_crossentropy')
model.fit(x,y, epochs=2, batch_size=2)
Output:
Epoch 1/2
50/50 [==============================] - 2s 4ms/step - loss: 0.2027
Epoch 2/2
50/50 [==============================] - 0s 4ms/step - loss: 0.1878