0
0
PyTorchml~10 mins

Text preprocessing for RNNs in PyTorch - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to tokenize the input sentence into words.

PyTorch
sentence = "I love machine learning"
tokens = sentence.[1]()
Drag options to blanks, or click blank then click option'
Asplit
Bjoin
Creplace
Dstrip
Attempts:
3 left
💡 Hint
Common Mistakes
Using join() instead of split() which combines words instead of splitting.
Using replace() which changes characters but does not split.
Using strip() which removes whitespace only at ends.
2fill in blank
medium

Complete the code to convert tokens to lowercase for uniformity.

PyTorch
tokens = ['I', 'Love', 'Machine', 'Learning']
lower_tokens = [word.[1]() for word in tokens]
Drag options to blanks, or click blank then click option'
Aupper
Blower
Ccapitalize
Dtitle
Attempts:
3 left
💡 Hint
Common Mistakes
Using upper() which converts to uppercase instead.
Using capitalize() which only changes the first letter.
Using title() which capitalizes each word.
3fill in blank
hard

Fix the error in the code to create a vocabulary dictionary mapping words to unique indices.

PyTorch
tokens = ['i', 'love', 'machine', 'learning', 'love']
vocab = {word: idx for idx, word in enumerate(set([1]))}
Drag options to blanks, or click blank then click option'
Alist
Bvocab
Ctokens
Drange
Attempts:
3 left
💡 Hint
Common Mistakes
Using vocab inside set() which is not defined yet.
Using list which is a type, not the variable.
Using range which is unrelated here.
4fill in blank
hard

Fill both blanks to convert a list of tokens into a list of indices using the vocabulary.

PyTorch
tokens = ['i', 'love', 'machine']
indices = [[1][word] for [2] in tokens]
Drag options to blanks, or click blank then click option'
Avocab
Bword
Ctokens
Dindices
Attempts:
3 left
💡 Hint
Common Mistakes
Using tokens instead of vocab to get indices.
Using tokens as loop variable which is the list, not element.
Using indices as loop variable which is the output list.
5fill in blank
hard

Fill all three blanks to pad sequences to the same length using PyTorch.

PyTorch
import torch
from torch.nn.utils.rnn import [1]

sequences = [torch.tensor([1, 2, 3]), torch.tensor([4, 5])]
padded = [2](sequences, batch_first=True, padding_value=[3])
Drag options to blanks, or click blank then click option'
Apad_sequence
Bpad_packed_sequence
Cpack_sequence
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using pack_sequence which packs sequences but does not pad.
Using pad_packed_sequence which unpacks padded sequences.
Using pack_padded_sequence which packs padded sequences.