Week 14 · Text Classification and Sentiment Analysis
English translation
Before practice
01 Convert sentences into a computable form
How should words and sentences be represented for a computer to classify sentence sentiment?
Description
Token ID: identifier of a word or Token
Embedding and classification model: learn representations useful for prediction
02 Sentences of different lengths
How should sentences of different lengths be grouped for computation together? How should padding added to equalize lengths be distinguished from actual words?
Description
Unnecessary Padding values affect the sentence representation. Exclude them with a Mask
Mean-based representations do not directly preserve word order
Practice
1. Tokens and vocabulary
Print two English sentences of different lengths in the order Token → Index → PAD.
Add one word absent from the training vocabulary and locate UNK.
Distinguish Indices for actual words from PAD and UNK.
2. Masked averaging and training
Run the mean comparison cell for the two provided sentences. Relative to the Vector from processing the short sentence alone, record the maximum absolute difference for the Vector processed with the long sentence, before and after applying the Mask.
In the comparison cell, change the sentence in single_ids and the first sentence in batch_ids to the same short sentence. Change the second sentence in batch_ids to a longer sentence and repeat the calculation. Record the sentences used and both differences. Keep the trained Embedding weights.
Write the Logits for two sentences with reordered words side by side and compute their maximum absolute difference. Using the Token count, PAD count, and printed maximum difference, explain separately the effect of including PAD in the average and the effect of word order under mean Pooling.
3. Enter new sentences
In my_reviews, write one positive, one neutral, and one negative English sentence using words from the training vocabulary.
Record the intended sentiment, prediction, and Tokens, and add one unknown word or punctuation mark to check UNK handling.
If the prediction differs from your intention, explain the cause.
Submission and saving
Include code, actual outputs and plots, and your own interpretation. Submit through KLAS; check the semester schedule and latest announcements for the deadline.
Filename: Name_StudentID_Week14.ipynb.
Restart the runtime, run all code from the top, then save.
Optional practice
When Token Indices are passed to Embedding as a Float Tensor
After training the sentence classification model, add a Code Cell at the bottom of the notebook and run the following code.
for debug_input in [X.float(), X]:
print("Token Index:", debug_input.shape, debug_input.dtype)
try:
with torch.no_grad():
print("Output:", model(debug_input).shape)
except (RuntimeError, ValueError) as error:
print(type(error).__name__, str(error))
In a Text Cell, tabulate each input's dtype and execution success. Explain the error in terms of Embedding inputs being Vocabulary row indices rather than continuous word measurements. Even after calling X.float(), the original X remains integer-valued.