TensorFlow processes text by breaking it into smaller pieces called tokens. Why is this step important?
Think about how computers work with numbers, not words.
Tokenization breaks text into pieces and maps them to numbers. Models work with numbers, so this step is essential.
You want to build a model that understands the order of words in sentences. Which TensorFlow model type is best?
Think about models that remember previous words to understand context.
RNNs process sequences by keeping track of previous inputs, making them suitable for text data.
Given this TensorFlow code snippet, what is the shape of the output tensor?
import tensorflow as tf vocab_size = 1000 embedding_dim = 16 sequence_length = 10 embedding_layer = tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=sequence_length) sample_input = tf.constant([[1, 5, 8, 0, 0, 0, 0, 0, 0, 0]]) output = embedding_layer(sample_input) print(output.shape)
Remember the embedding layer output shape is (batch_size, input_length, embedding_dim).
The input has batch size 1 and sequence length 10. The embedding dimension is 16, so output shape is (1, 10, 16).
You trained a TensorFlow model to classify movie reviews as positive or negative. Which metric best shows how well the model predicts the correct class?
Think about a metric that counts correct predictions in classification.
Accuracy measures the percentage of correct predictions, making it suitable for classification tasks.
Consider this TensorFlow code snippet for training a text model. It raises a shape mismatch error. What is the cause?
import tensorflow as tf vocab_size = 5000 embedding_dim = 32 sequence_length = 20 model = tf.keras.Sequential([ tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=sequence_length), tf.keras.layers.GlobalAveragePooling1D(), tf.keras.layers.Dense(1, activation='sigmoid') ]) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # Input data shape is (batch_size, sequence_length) inputs = tf.random.uniform((64, 10), maxval=vocab_size, dtype=tf.int32) labels = tf.random.uniform((64, 1), maxval=2, dtype=tf.int32) model.fit(inputs, labels, epochs=1)
Check if the input data shape matches the model's expected input shape.
The model expects input sequences of length 20, but the input data has length 10, causing a shape mismatch error.
