You want to classify short text messages into categories. Which TensorFlow model architecture is best suited for this task?
Think about models that understand sequences of words.
RNNs and LSTMs are designed to handle sequences like text, capturing word order and context, which is important for text classification.
Given this TensorFlow code snippet for text classification, what is the shape of embedded_sequences?
import tensorflow as tf vocab_size = 10000 embedding_dim = 16 max_length = 20 model = tf.keras.Sequential([ tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length) ]) input_example = tf.constant([[1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) embedded_sequences = model(input_example) print(embedded_sequences.shape)
The embedding layer outputs one vector per input word, preserving batch size and sequence length.
The input shape is (batch_size=1, sequence_length=20). The embedding layer converts each word index to a 16-dimensional vector, so output shape is (1, 20, 16).
You are training a text classification model on a large dataset. Which batch size choice is likely to improve training stability and speed without using too much memory?
Consider a balance between memory use and stable gradient estimates.
Batch sizes like 32 or 64 provide a good balance between training speed, memory use, and stable gradient updates for text models.
You trained a text classification model with 5 categories. After evaluation, your accuracy is 0.85. What does this accuracy mean?
Accuracy measures how many predictions match the true labels.
Accuracy is the fraction of total predictions where the predicted category equals the true category.
What error will this TensorFlow code raise when run?
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Embedding(input_dim=5000, output_dim=16),
tf.keras.layers.GlobalAveragePooling1D(),
tf.keras.layers.Dense(1, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])Check if the output layer activation matches the number of classes.
Softmax activation expects multiple output units for multi-class classification. Using softmax with one unit causes a ValueError.
