Reputation: 306
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
Reputation: 2730
There are some problems with the code:
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
Reputation: 6367
You can not use your model with tensor with ragged last dimension (where the lengths of sentences are different).
Upvotes: 1