简体   繁体   中英

Tensorflow RNN Model Shapes are Incompatible Error

I'm trying to train a RNN classifier over some text data.

Facts:

  • I've been trying to use https://www.tensorflow.org/text/tutorials/text_classification_rnn as an example, however the example uses data downloaded through the tensorflow_dataset package while I have a homegrown data set.
  • I feed in a tensor of texts (X) and one-hot encoded arrays (Y).
  • I deviated from the example by changing the loss to Categorical Cross Entropy, and I set the final Dense layer to have an activation of softmax and dimension of 32. 32 is the length of each one-hot encoded line. I figured that's what I wanted the final output to be.
  • I get an error: ValueError: Shapes (None, 1) and (None, 32) are incompatible

Question:

Why would I get the shape of 1 anywhere in this model? Is there something that I omitted?

model = tf.keras.Sequential([
    train_encoder,
    tf.keras.layers.Embedding(
        input_dim=len(train_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(32, activation='softmax')
])

model.compile(loss=tf.keras.losses.CategoricalCrossentropy(),
              optimizer=tf.keras.optimizers.Adam(1e-4),
              metrics=['accuracy'])

model.fit(x, y_indexes, epochs=50, verbose=2, validation_split=0.1)

The issue is definitely in the dataset: (None, 32) is the shape of the model output, while (None, 1) is the shape of the provided y_indexes variable.

Perhaps you didn't actually one-hot encoded the labels, in which case just do it or use SparseCategoricalCrossentropy() as the loss function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM