Hi @para, it is good practice format pasted code with the preformatted text option and make sure that the indentation is correct. Otherwise, your code is hart to read and other people are probably less likely to try to understand it. That means: make it as easy as possible for others to help you by giving enough context for the problem and only include relevant and readable code.
Tip: you can still edit your previous post and format the code example in the right way. (see image)

formatting1520×310 50.9 KB

Now looking at your code…
class Dataset(torch.utils.data.Dataset):
  def __init__ (self, encodings, labels=None):
    self.encodings = encodings
    self.labels = labels
  def __getitem__ (self, idx):
    item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
    if self.labels:
      item[“labels”] = torch.tensor(self.labels[idx])
      return item
  def __len__ (self):
    return len(self.encodings[“input_ids”])

train_dataset = Dataset(X_train_tokenized, y_train)
val_dataset = Dataset(X_val_tokenized, y_val)
args = TrainingArguments(
  evaluation_strategy=“steps”,
  eval_steps=500,
  per_device_train_batch_size=8,
  per_device_eval_batch_size=8,
  num_train_epochs=1,
  seed=0,
  load_best_model_at_end=True,
)
trainer = Trainer(
  model=model,
  args=args,
  train_dataset=train_dataset,
  eval_dataset=val_dataset,
  compute_metrics=compute_metrics,)

I actually have several questions:

What model are you using?
What dataset are you using?

How many different categories does it have?
What is the highest and lowest frequency of categories?
How many examples do you have in training and validation set?


what evaluation metrics are you using?
what results do you get for your metrics at the end of training?
did you look at training and validation loss curves to judge if your model is overfitting?
did you try different values for num_train_epochs and learning_rate?

Actually, training for 1 epoch is only good for getting some preliminary results. You should try training with 2-4 epochs and different learning rates to get better results before trying out class weights.
But to answer your initial question:
Your can copy the code posted above and then replace the line
trainer = Trainer(

with
trainer = CustomTrainer(

That should work.