Dom DJ
Dom DJ

Reputation: 21

ValueError: Input 0 of layer bidirectional_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (13, 64)

I have made a model and have got this error (it is a kaggle notebook), this is the error:

    ValueError: Input 0 of layer bidirectional_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (13, 64)

        <ipython-input-25-f223adf5c5a7> in <module>
             12   for i in range(len(scores_transformed_train)):
             13     # Optimize the model
        ---> 14     loss_value, grads = grad(temp_model, title_train[i], 
    scores_transformed_train[i])
             15     optimizer.apply_gradients(zip(grads, temp_model.trainable_variables))
             16 

The model:

    encoder = tf.keras.layers.experimental.preprocessing.TextVectorization()

    encoder.adapt(title_train)

    model = tf.keras.Sequential([
        encoder,
        tf.keras.layers.Embedding(
            input_dim=len(encoder.get_vocabulary()),
            output_dim=64,
            mask_zero=True),
        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),
        tf.keras.layers.Dense(64, activation='relu'),
        tf.keras.layers.Dense(1, 'sigmoid')
    ])

my functions

    loss_object = tf.keras.losses.MeanSquaredError()
    
    def loss(model, x, y, training):
      y_ = model(x, training=training)

      return loss_object(y_true=y, y_pred=y_)


    def grad(model, inputs, targets):
      with tf.GradientTape() as tape:
        loss_value = loss(model, inputs, targets, training=True)
      return loss_value, tape.gradient(loss_value, model.trainable_variables)

    optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)

Please help me fix it. I will be happy to answer any additional queries if needed...

Upvotes: 2

Views: 1620

Answers (2)

Arman Asgharpoor
Arman Asgharpoor

Reputation: 331

You should use Reshape layer after Bidirectional layer

Some thing like this:

tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),
tf.keras.layers.Reshape((128, 1), input_shape = (128, )),
tf.keras.layers.Dense(64, activation='relu'),

Upvotes: 0

but-why
but-why

Reputation: 583

Usually the expected dimension in such model is 3. (time x batch x features). You provided a 2d.

Upvotes: 1

Related Questions