|
|
import torch
|
|
|
from torch.utils.data import Dataset
|
|
|
|
|
|
from torch.utils.data import Dataset
|
|
|
import json
|
|
|
|
|
|
|
|
|
class English2HindiDataset(Dataset):
|
|
|
def __init__(
|
|
|
self, data, tokenizer_src, tokenizer_tgt, src_lang, tgt_lang, seq_len
|
|
|
):
|
|
|
super().__init__()
|
|
|
self.seq_len = seq_len
|
|
|
self.tokenizer_src = tokenizer_src
|
|
|
self.tokenizer_tgt = tokenizer_tgt
|
|
|
self.src_lang = src_lang
|
|
|
self.tgt_lang = tgt_lang
|
|
|
|
|
|
self.data = data
|
|
|
self.sos_token = torch.tensor(
|
|
|
[tokenizer_tgt.token_to_id("[SOS]")], dtype=torch.int64
|
|
|
)
|
|
|
self.eos_token = torch.tensor(
|
|
|
[tokenizer_tgt.token_to_id("[EOS]")], dtype=torch.int64
|
|
|
)
|
|
|
self.pad_token = torch.tensor(
|
|
|
[tokenizer_tgt.token_to_id("[PAD]")], dtype=torch.int64
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
return len(self.data)
|
|
|
|
|
|
def causal_mask(self,size):
|
|
|
mask = torch.triu(torch.ones((1, size, size)), diagonal=1).type(torch.int)
|
|
|
return mask == 0
|
|
|
|
|
|
def __getitem__(self, idx):
|
|
|
trans_pairs = self.data[idx]
|
|
|
|
|
|
src_text = trans_pairs["en_text"]
|
|
|
tgt_text = trans_pairs["hi_text"]
|
|
|
|
|
|
|
|
|
enc_input_tokens = self.tokenizer_src.encode(src_text).ids
|
|
|
dec_input_tokens = self.tokenizer_tgt.encode(tgt_text).ids
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enc_num_padding_tokens = self.seq_len - len(enc_input_tokens) - 2
|
|
|
|
|
|
|
|
|
|
|
|
dec_num_padding_tokens = self.seq_len - len(dec_input_tokens) - 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
encoder_input = torch.cat(
|
|
|
[
|
|
|
self.sos_token,
|
|
|
torch.tensor(enc_input_tokens, dtype=torch.int64),
|
|
|
self.eos_token,
|
|
|
torch.tensor(
|
|
|
[self.pad_token] * enc_num_padding_tokens, dtype=torch.int64
|
|
|
),
|
|
|
],
|
|
|
dim=0,
|
|
|
)
|
|
|
|
|
|
decoder_input = torch.cat(
|
|
|
[
|
|
|
self.sos_token,
|
|
|
torch.tensor(dec_input_tokens, dtype=torch.int64),
|
|
|
torch.tensor(
|
|
|
[self.pad_token] * dec_num_padding_tokens, dtype=torch.int64
|
|
|
),
|
|
|
],
|
|
|
dim=0,
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
label = torch.cat(
|
|
|
[
|
|
|
torch.tensor(dec_input_tokens, dtype=torch.int64),
|
|
|
self.eos_token,
|
|
|
torch.tensor(
|
|
|
[self.pad_token]* dec_num_padding_tokens, dtype=torch.int64
|
|
|
),
|
|
|
],
|
|
|
dim=0,
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
encoder_mask = (encoder_input != self.pad_token).unsqueeze(0).unsqueeze(0).int()
|
|
|
decoder_mask= (decoder_input != self.pad_token).unsqueeze(0).int() & self.causal_mask(decoder_input.size(0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
"encoder_input": encoder_input,
|
|
|
"decoder_input": decoder_input,
|
|
|
"encoder_mask": encoder_mask,
|
|
|
"decoder_mask": decoder_mask,
|
|
|
"label": label,
|
|
|
"src_text": src_text,
|
|
|
"tgt_text": tgt_text,
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class English2HindiDatasetTest(Dataset):
|
|
|
def __init__(
|
|
|
self, json_path,
|
|
|
):
|
|
|
super().__init__()
|
|
|
with open(json_path, "r", encoding="utf-8") as f:
|
|
|
self.data = json.load(f)
|
|
|
|
|
|
def __len__(self):
|
|
|
return len(self.data)
|
|
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, idx):
|
|
|
trans_pairs = self.data[idx]
|
|
|
return trans_pairs |