Hi,
I am facing issue. pls help
Labels shape: torch.Size([4])
inputs shape: torch.Size([4, 512])
ValueError Traceback (most recent call last)
in <cell line: 14>()
20 print(“Labels shape:”, labels.shape)
21 print(“inputs shape:”, inputs.shape)
—> 22 outputs = model(inputs, attention_mask=attention_mask, labels=labels)
23 loss = outputs.loss # Use the loss directly from the model’s output
24 loss.backward()
9 frames
/usr/local/lib/python3.10/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)
3057 if size_average is not None or reduce is not None:
3058 reduction = _Reduction.legacy_get_string(size_average, reduce)
→ 3059 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
3060
3061
ValueError: Expected input batch_size (2048) to match target batch_size (4).
code
class NERDataset(Dataset):
def init(self, texts, labels, tokenizer,label_map):
self.texts = texts
self.labels = labels
self.tokenizer = tokenizer
self.label_map=label_map
def __len__(self):
return len(self.texts)
def encode_label_to_id(self,label):
return self.label_map[label]
def __getitem__(self, idx):
text = self.texts[idx]
label = self.labels[idx]
encoding = self.tokenizer(text, truncation=True, padding='max_length', return_tensors='pt', max_length=512)
# Tokenize the text and convert to input features
# Extract input_ids and attention_mask
input_ids = encoding['input_ids'].flatten()
attention_mask = encoding['attention_mask'].flatten()
label_id =self.encode_label_to_id(label)
return {
'input_ids': input_ids,
'attention_mask': attention_mask,
'labels': torch.tensor(label_id, dtype=torch.long) # Use the encoded label
}
train_dataset = NERDataset(train_texts, train_labels, tokenizer,label_map)
val_dataset = NERDataset(val_texts, val_labels, tokenizer,label_map)
batch_size = 4 # Adjust as needed
train_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
val_dataloader = DataLoader(val_dataset, batch_size=batch_size)
import torch
from transformers import AdamW
from transformers import get_linear_schedule_with_warmup
import numpy as np
optimizer = AdamW(model.parameters(), lr=5e-5) # Example learning rate, adjust as needed
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=0, num_training_steps=len(train_dataset))
Define your loss function
loss_fn = torch.nn.CrossEntropyLoss()
Fine-tune the model
model.train()
for epoch in range(1):
for batch in train_dataloader:
optimizer.zero_grad()
inputs = batch[‘input_ids’]
attention_mask = batch[‘attention_mask’]
labels = batch[‘labels’]
print(“Labels shape:”, labels.shape)
print(“inputs shape:”, inputs.shape)
outputs = model(inputs, attention_mask=attention_mask, labels=labels)
loss = outputs.loss # Use the loss directly from the model’s output
loss.backward()
optimizer.step()
scheduler.step()
Evaluate the fine-tuned model
model.eval()
for batch in val_dataloader:
inputs, labels = batch
with torch.no_grad():
outputs = model(**inputs)