Sorry, here is the code that produces the error: “Indexing with integers (to access backend Encoding for a given batch index) is not available when using Python based tokenizers”
<—>
from sklearn.model_selection import train_test_split
from transformers import BartTokenizer
tokenizer = BartTokenizer.from_pretrained(‘facebook/bart-large-cnn’)
train_texts, val_texts, train_labels, val_labels = train_test_split(df.articles, df.highlights, test_size=.2)
train_texts = train_texts.values.tolist()
train_labels = train_labels.values.tolist()
val_texts = val_texts.values.tolist()
val_labels = val_labels.values.tolist()
train_encodings = tokenizer(train_texts, truncation=True, padding=True)
train_label_encodings = tokenizer(train_labels, truncation=True, padding=True)
val_encodings = tokenizer(val_texts, truncation=True, padding=True)
val_label_encodings = tokenizer(val_labels, truncation=True, padding=True)
import torch
class PyTorchDatasetCreate(torch.utils.data.Dataset):
def init(self, encodings, labels):
self.encodings = encodings
self.labels = labels
def __getitem__(self, idx):
item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
item['labels'] = torch.tensor(self.labels[idx])
return item
def __len__(self):
return len(self.labels)
train_dataset = PyTorchDatasetCreate(train_encodings, train_label_encodings)
val_dataset = PyTorchDatasetCreate(val_encodings, val_label_encodings)
from transformers import BartForConditionalGeneration, Trainer, TrainingArguments
training_args = TrainingArguments(
output_dir=’./results’, # output directory
num_train_epochs=3, # total number of training epochs
per_device_train_batch_size=1, # batch size per device during training
per_device_eval_batch_size=1, # batch size for evaluation
warmup_steps=200, # number of warmup steps for learning rate scheduler
weight_decay=0.01, # strength of weight decay
logging_dir=’./logs’, # directory for storing logs
logging_steps=10)
model = BartForConditionalGeneration.from_pretrained(“sshleifer/distilbart-cnn-12-6”)
trainer = Trainer(
model=model, # the instantiated
Transformers model to be trained
args=training_args, # training arguments, defined above
train_dataset=train_dataset, # training dataset
eval_dataset=val_dataset) # evaluation dataset
trainer.train()
<---->
The code for the other error ("TypeError: new(): invalid data type ‘str’) is identical except the labels (train and val) are not tokenized. Appreciate your help!