Data Preperation

Visulize the data used for training Eng-Arb translation experiments.

Show Code
from data_pipeline import (
    train_loader, test_pairs,
    english_word_to_index, english_index_to_word,
    arabic_word_to_index, arabic_index_to_word,
    tokenize, encode_source, PAD_IDX,
)

src_batch, dec_in_batch, dec_tgt_batch, src_lengths = next(iter(train_loader))

The data we will be using has the follwing for each load (src_batch, dec_in_batch, dec_tgt_batch, src_lengths): - serc_batch: The source sentence (English) with padding - dec_in_batch: The decoder input sentence (Arabic) with padding - dec_tgt_batch: The decoder target sentence (Arabic) with padding - src_lengths: The actual length of the source sentence (without padding)

Below we will visualize the data for a single batch.

Show Code
# Here is the shape for each:
print(f"src_batch shape: {src_batch.shape}")
print(f"dec_in_batch shape: {dec_in_batch.shape}")
print(f"dec_tgt_batch shape: {dec_tgt_batch.shape}")
print(f"src_lengths shape: {src_lengths.shape}")
src_batch shape: torch.Size([32, 13])
dec_in_batch shape: torch.Size([32, 10])
dec_tgt_batch shape: torch.Size([32, 10])
src_lengths shape: torch.Size([32])
Show Code
WORD_IND = 10

#print the first source sentence in the batch
print("First source sentence in the batch:")
print(src_batch[WORD_IND])
#print the first decoder input sentence in the batch
print("First decoder input sentence in the batch:")
print(dec_in_batch[WORD_IND])
#print the first decoder target sentence in the batch
print("First decoder target sentence in the batch:")
print(dec_tgt_batch[WORD_IND])
First source sentence in the batch:
tensor([107, 106, 378, 703,   2,   0,   0,   0,   0,   0,   0,   0,   0])
First decoder input sentence in the batch:
tensor([   1,  110,  195,  972,  862, 1513,    0,    0,    0,    0])
First decoder target sentence in the batch:
tensor([ 110,  195,  972,  862, 1513,    2,    0,    0,    0,    0])
Show Code
# print the first source sentence in the batch as words
print("First source sentence in the batch as words:")
print([english_index_to_word[idx.item()]
      for idx in src_batch[WORD_IND] if idx.item() != PAD_IDX])
# print the first decoder input sentence in the batch as words
print("First decoder input sentence in the batch as words:")
print([arabic_index_to_word[idx.item()] for idx in dec_in_batch[WORD_IND]
       if idx.item() != PAD_IDX])
# print the first decoder target sentence in the batch as words
print("First decoder target sentence in the batch as words:")
print([arabic_index_to_word[idx.item()]
      for idx in dec_tgt_batch[WORD_IND] if idx.item() != PAD_IDX])
First source sentence in the batch as words:
['that', 'was', 'years', 'ago', '<eos>']
First decoder input sentence in the batch as words:
['<sos>', 'ูƒุงู†', 'ุฐู„ูƒ', 'ู…ู†ุฐ', 'ุนุฏุฉ', 'ุณู†ูŠู†']
First decoder target sentence in the batch as words:
['ูƒุงู†', 'ุฐู„ูƒ', 'ู…ู†ุฐ', 'ุนุฏุฉ', 'ุณู†ูŠู†', '<eos>']
Show Code
#print 30 decoder input sentences in the batch as words
for i in range(30):
    print(f"Decoder input sentence {i} in the batch as words:")
    print([arabic_index_to_word[idx.item()]
          for idx in dec_in_batch[i] if idx.item() != PAD_IDX])
Decoder input sentence 0 in the batch as words:
['<sos>', 'ุฅู†ู‡ุง', 'ุตุบูŠุฑุฉ', 'ุฌุฏุง']
Decoder input sentence 1 in the batch as words:
['<sos>', '<unk>', 'ู„ูŠ', 'ุนู†', 'ุฑุญู„ุฉ', 'ุณูุฑูƒ', 'ู…ู†', 'ูุถู„ูƒ']
Decoder input sentence 2 in the batch as words:
['<sos>', 'ู„ุง', 'ู…ุดูƒู„ุฉ', 'ู…ุทู„ู‚ุง', '!']
Decoder input sentence 3 in the batch as words:
['<sos>', 'ู‡ุฐุง', 'ูŠุคู„ู…']
Decoder input sentence 4 in the batch as words:
['<sos>', 'ุขู…ู„', 'ุฃู„ุง', '<unk>', 'ู†ูุณ', 'ุฎุทุฃ', 'ุชูˆู…']
Decoder input sentence 5 in the batch as words:
['<sos>', 'ูƒูŠู', 'ุชุนู„ู…', '<unk>', 'ุชูˆู…', 'ู„ู‡ุฐุง', 'ุŸ']
Decoder input sentence 6 in the batch as words:
['<sos>', 'ู†ุญู†', 'ู„ุง', '<unk>', 'ุงู„ุฅู†ุชุธุงุฑ', 'ู„ู„ุนุดุงุก']
Decoder input sentence 7 in the batch as words:
['<sos>', 'ุฅู†ู‡', 'ูŠุฃูƒู„']
Decoder input sentence 8 in the batch as words:
['<sos>', 'ู…ุง', 'ู‡ูŠ', '<unk>', 'ุนู…ู„ูƒ', 'ููŠ', 'ุงู„ู…ูƒุชุจ', 'ุŸ']
Decoder input sentence 9 in the batch as words:
['<sos>', 'ุฐู‡ุจุช', 'ุฃูŠุถุง']
Decoder input sentence 10 in the batch as words:
['<sos>', 'ูƒุงู†', 'ุฐู„ูƒ', 'ู…ู†ุฐ', 'ุนุฏุฉ', 'ุณู†ูŠู†']
Decoder input sentence 11 in the batch as words:
['<sos>', 'ู„ุง', 'ุชู‚ู„ู‚', 'ุฃู†ุง', '<unk>']
Decoder input sentence 12 in the batch as words:
['<sos>', 'ุฃุญุชุงุฌ', 'ุงู„ุชุญุฏุซ', 'ู…ุนูƒ', 'ุนู†', 'ุฃู…ุฑ', '<unk>']
Decoder input sentence 13 in the batch as words:
['<sos>', 'ุฃู†ุง', 'ู„ุง', 'ุฃุธู†', 'ุฃู†ู‡ุง', 'ุชุดุจู‡', 'ุฃู…ู‡ุง']
Decoder input sentence 14 in the batch as words:
['<sos>', 'ุนู†ุฏ', 'ูƒู„', 'ุงู„ู†ุงุณ', '<unk>', 'ู…ู†', '<unk>', 'ุงู„ูŠูˆู…']
Decoder input sentence 15 in the batch as words:
['<sos>', 'ู‡ู„', 'ู…ู†', '<unk>', 'ูŠุง', 'ุณูŠุฏูŠ', 'ุŸ']
Decoder input sentence 16 in the batch as words:
['<sos>', 'ุงู„ุญุจ', 'ุฃุนู…ู‰']
Decoder input sentence 17 in the batch as words:
['<sos>', 'ุฃุตู„ุญุช', 'ุงู„ุฏุฑุงุฌุฉ', 'ุจุงู„ุฃู…ุณ']
Decoder input sentence 18 in the batch as words:
['<sos>', 'ู‡ูˆ', 'ู…ูˆู‡ูˆุจ', 'ุฌุฏุง']
Decoder input sentence 19 in the batch as words:
['<sos>', 'ู†ุตุญุชู‡', 'ุฃู„ุง', 'ูŠุฃูƒู„', 'ูƒุซูŠุฑุง']
Decoder input sentence 20 in the batch as words:
['<sos>', 'ุฃุฎุจุฑู†ูŠ', 'ุชูˆู…', '<unk>', 'ุฃูุนู„', 'ุฐู„ูƒ', 'ุฃุจุฏุง']
Decoder input sentence 21 in the batch as words:
['<sos>', 'ู„ู…', 'ุฃุญุตู„', 'ุนู„ู‰', 'ุฎูŠุงุฑ']
Decoder input sentence 22 in the batch as words:
['<sos>', 'ู‡ู„', 'ู†ุณูŠุช', 'ุฅู‚ูุงู„', 'ุงู„ุจุงุจ', 'ุงู„ู„ูŠู„ุฉ', 'ุงู„ู…ุงุถูŠุฉ', 'ุŸ']
Decoder input sentence 23 in the batch as words:
['<sos>', 'ูŠุตุนุจ', 'ุนู„ูŠ', 'ู‚ุฑุงุกุฉ', 'ู‡ุฐุง', 'ุงู„ูƒุชุงุจ']
Decoder input sentence 24 in the batch as words:
['<sos>', 'ุฃู†ุช', 'ุฏุงุฆู…ุง', 'ู…ุง', '<unk>']
Decoder input sentence 25 in the batch as words:
['<sos>', 'ู„ุง', 'ุฃุนุฑู', 'ูƒูŠู', 'ู„ูƒู†ูŠ', '<unk>']
Decoder input sentence 26 in the batch as words:
['<sos>', 'ู…ู†', 'ุฃู†ุช', 'ุŸ']
Decoder input sentence 27 in the batch as words:
['<sos>', '<unk>', 'ุชูˆู…', 'ูƒุซูŠุฑุง', 'ุฅู†', 'ุนุฑู', 'ุฃู†ูŠ', 'ู‚ู„ุช', 'ู„ูƒ', 'ู‡ุฐุง']
Decoder input sentence 28 in the batch as words:
['<sos>', 'ู‡ู„', 'ู‡ุฐู‡', '<unk>', 'ุŸ']
Decoder input sentence 29 in the batch as words:
['<sos>', 'ุงู„ุฐูŠ', 'ู‚ู„ุชู‡', 'ูƒุงู†', 'ูŠุฌุจ', 'ุฃู†', 'ูŠู‚ุงู„']

Comments