Skip to content

Instantly share code, notes, and snippets.

@rajy4683
Created February 7, 2021 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajy4683/8cc16334f1db979cb9a4d7814280e1d2 to your computer and use it in GitHub Desktop.
Save rajy4683/8cc16334f1db979cb9a4d7814280e1d2 to your computer and use it in GitHub Desktop.
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
BATCH_SIZE = 128 ### Hyperparameter
"""
Defines an iterator that batches examples of similar lengths together thereby minimizes the padding per batch.
"""
train_iterator, valid_iterator, test_iterator = BucketIterator.splits(
(train_data, valid_data, test_data),
batch_size = BATCH_SIZE,
device = device)
### Single sample from train_iterator
sample_from_iterator = next(iter(train_iterator))
print(sample_from_iterator)
print("Fields in the iterator", sample_from_iterator.fields)
print("Shape of ", sample_from_iterator.src.shape, sample_from_iterator.trg.shape)
#### Output
"""
[torchtext.data.batch.Batch of size 128 from MULTI30K]
[.src]:[torch.cuda.LongTensor of size 128x33 (GPU 0)]
[.trg]:[torch.cuda.LongTensor of size 128x35 (GPU 0)]
Fields in the iterator dict_keys(['src', 'trg'])
Shape of torch.Size([128, 33]) torch.Size([128, 35])
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment