Anatole Sot
Anatole Sot

Reputation: 306

Keras Tensor Flow Error : ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None`

It's my first time using Tensorflow and I have this code :

import tensorflow as tf
# Task: predict whether each sentence is a question or not.
sentences = tf.constant(
    ['What makes you think she is a witch?',
     'She turned me into a newt.',
     'A newt?',
     'Well, I got better.'])
is_question = tf.constant([True, False, True, False])

# Build the Keras model.
keras_model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=[None], ragged=True),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10)
])

keras_model.compile(loss='mse', optimizer='rmsprop',metrics=['accuracy'])
keras_model.fit(sentences, is_question, epochs=5)
test_loss, test_acc = keras_model.evaluate(sentences,  is_question)

print('\nTest accuracy:', test_acc)

But when I try to use it I have this error :

ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None`.

Upvotes: 0

Views: 1503

Answers (2)

Marzi Heidari
Marzi Heidari

Reputation: 2730

There are some problems with the code:

  1. You need to specify the shape of the input layer.
  2. You cannot feed raw text to a deep model. You need to tokenize it to integers.
  3. (The one that actually raises mentioned error:) You cannot feed the model the input that has ragged last dimension. You need to pad the input with zeros to reach a constant sequence length.

for tokenizing and padding you can use the following code:

from keras.preprocessing.text import Tokenizer
tokenizer = Tokenizer(num_words=my_max)
tokenizer.fit_on_texts(text) 
sequences = tokenizer.texts_to_sequences(text)   
sequences_matrix = sequence.pad_sequences(sequences, maxlen=max_sequence_length,
                                           truncating='post', padding='post')

Upvotes: 3

Andrey
Andrey

Reputation: 6367

You can not use your model with tensor with ragged last dimension (where the lengths of sentences are different).

Upvotes: 1

Related Questions