vaibhav bhatt
vaibhav bhatt

Reputation: 1

'bert-base-multilingual-uncased' dataloader RuntimeError : stack expects each tensor to be equal size

I am a begineer in nlp, as I was giving this competition https://www.kaggle.com/c/contradictory-my-dear-watson I am using the model 'bert-base-multilingual-uncased' and using BERT tokenizer from the same. I am also using kaggle tpu. This is the custom dataloader I created.

class SherlockDataset(torch.utils.data.Dataset):

def __init__(self,premise,hypothesis,tokenizer,max_len,target = None):
    super(SherlockDataset,self).__init__()
    self.premise = premise
    self.hypothesis = hypothesis
    self.tokenizer = tokenizer
    self.max_len = max_len
    self.target = target

def __len__(self):
    return len(self.premise)

def __getitem__(self,item):
    sen1 = str(self.premise[item])
    sen2 = str(self.hypothesis[item])
    
    encode_dict = self.tokenizer.encode_plus(sen1,
                                        sen2,
                                        add_special_tokens = True,
                                        max_len = self.max_len,
                                        pad_to_max_len = True,
                                        return_attention_mask = True,
                                        return_tensors = 'pt'
                                       )
    input_ids = encode_dict["input_ids"][0]
    token_type_ids = encode_dict["token_type_ids"][0]
    att_mask = encode_dict["attention_mask"][0]
    
    if self.target is not None:
        sample = {
        "input_ids":input_ids,
        "token_type_ids":token_type_ids,
        "att_mask":att_mask,
        "targets": self.target[item]
        }
    else:
        sample = {
        "input_ids":input_ids,
        "token_type_ids":token_type_ids,
        "att_mask":att_mask
        }
    
    return sample

and during the time of loading data in dataloader

def train_fn(model,dataloader,optimizer,criterion,scheduler = None):
model.train()
print("train")
for idx, sample in enumerate(dataloader):
    '''
    input_ids = sample["input_ids"].to(config.DEVICE)
    token_type_ids = sample["token_type_ids"].to(config.DEVICE)
    att_mask = sample["att_mask"].to(config.DEVICE)
    targets = sample["targets"].to(config.DEVICE)
    '''
    print("train_out")
    input_ids = sample[0].to(config.DEVICE)
    token_type_ids = sample[1].to(config.DEVICE)
    att_mask = sample[2].to(config.DEVICE)
    targets = sample[3].to(config.DEVICE)
    
    optimizer.zero_grad()
    output = model(input_ids,token_type_ids,att_mask)
    output = np.argmax(output,axis = 1)
    loss = criterion(outputs,targets)
    accuracy = accuracy_score(output,targets)
    loss.backward()
    torch.nn.utils.clip_grad_norm_(model.parameters(),1.0)
    xm.optimizer_step(optimizer, barrier=True)
    if scheduler is not None:
        scheduler.step()
    if idx%50==0:
        print(f"idx : {idx}, TRAIN LOSS : {loss}")

I am facing this error again and again

RuntimeError: Caught RuntimeError in DataLoader worker process 0. Original Traceback (most recent 
call last): File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 
178, 
in _worker_loop data = fetcher.fetch(index) File "/opt/conda/lib/python3.7/site- 
packages/torch/utils/data/_utils/fetch.py", line 47, in fetch return self.collate_fn(data) File 
"/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py", line 79, in 
 default_collate return [default_collate(samples) for samples in transposed] File 
"/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py", line 79, in return 
 [default_collate(samples) for samples in transposed] File "/opt/conda/lib/python3.7/site- 
 packages/torch/utils/data/_utils/collate.py", line 55, in default_collate return torch.stack(batch, 
 0, out=out) RuntimeError: stack expects each tensor to be equal size, but got [47] at entry 0 and 
 [36] at entry 1

I have tried changing num_workers values,changing batch sizes. I have checked the data and none of the text in it is null, 0 or corrupt in any sense. I have also tried changing max_len in tokenizer but I am not able to find out solution to this problem. Please check and let me know how can I fix it.

Upvotes: 0

Views: 722

Answers (1)

Queraa
Queraa

Reputation: 1

data_loader = torch.utils.data.DataLoader( batch_size=batch_size, dataset=data, shuffle=shuffle, num_workers=0, collate_fn=lambda x: x )

Use of Collate_fn in dataloader should be able to solve the problem.

Upvotes: 0

Related Questions