0
0
PyTorchml~10 mins

Custom Dataset class 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 import the Dataset class from PyTorch.

PyTorch
from torch.utils.data import [1]
Drag options to blanks, or click blank then click option'
ADataset
BDataLoader
CTensor
DModule
Attempts:
3 left
💡 Hint
Common Mistakes
Importing DataLoader instead of Dataset
Trying to import Dataset from torch instead of torch.utils.data
2fill in blank
medium

Complete the code to define the __len__ method that returns the dataset size.

PyTorch
def __len__(self):
    return [1]
Drag options to blanks, or click blank then click option'
Aself.data.size()
Bself.data.shape
Clen(self.data)
Dself.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using self.data.size() which is not valid for lists
Returning self.length without defining it
3fill in blank
hard

Fix the error in the __getitem__ method to return the correct sample.

PyTorch
def __getitem__(self, idx):
    sample = self.data[[1]]
    return sample
Drag options to blanks, or click blank then click option'
A0
Bidx + 1
Cself.idx
Didx
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 1 to idx causing index out of range
Using self.idx which is undefined
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths for words longer than 3 characters.

PyTorch
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword != ''
Dword.isalpha()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of len(word) as value
Using wrong condition like word != '' which does not filter by length
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths for words longer than 4 characters.

PyTorch
result = [1]: [2] for word in words if [3]
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Clen(word) > 4
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.lower() instead of word.upper() for keys
Filtering with wrong length condition