-
-
Save rajy4683/8cc16334f1db979cb9a4d7814280e1d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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