here is another script from different tutorial with the same problem…
Import the Libraries:
from transformers import BertTokenizer, BertForSequenceClassification
import torch, time
import torch.optim as optim
import torch.nn as nn
from sklearn.metrics import f1_score, accuracy_score

import random
import numpy as np
import pandas as pd
from torchtext import data
from torchtext.data import TabularDataset
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

Prepare Data:
model_class, tokenizer_class, pretrained_weights = (BertForSequenceClassification, BertTokenizer, 'bert-base-cased')
tokenizer = tokenizer_class.from_pretrained(pretrained_weights)
model = model_class.from_pretrained(pretrained_weights)
df_train = pd.read_csv("/content/drive/My Drive/train_set.csv")
df_val = pd.read_csv("/content/drive/My Drive/val_set.csv")
df = pd.concat([df_train, df_val], axis=0)

tokenized = df_train['text'].apply((lambda x: tokenizer.encode(x, add_special_tokens=True)))

max_len = 0
for i in tokenized.values:
    if len(i) > max_len:
        max_len = len(i)

padded = np.array([i + [0]*(max_len-len(i)) for i in tokenized.values])
attention_mask = np.where(padded != 0, 1, 0)

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
class Dataset(torch.utils.data.Dataset):
  def __init__(self, input_ids, attention_masks, labels):
        self.input_ids = input_ids
        self.attention_mask = attention_masks
        self.labels = labels

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

  def __getitem__(self, index):
        input_ids = self.input_ids[index]
        attention_mask = self.attention_mask[index]
        labels = self.labels[index]
        return input_ids, attention_mask, labels

train_input_ids = torch.tensor(padded[:len(df_val)])  
train_attention_mask = torch.tensor(attention_mask[:len(df_val)])
train_labels = list(df['label'][:len(df_val)])

validation_input_ids = torch.tensor(padded[len(df_val):])  
validation_attention_mask = torch.tensor(attention_mask[len(df_val):])
validation_labels = list(df['label'][len(df_val):])


dataset = Dataset(input_ids=train_input_ids, attention_masks=train_attention_mask, labels=train_labels)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=8, drop_last=False)

validation_dataset = Dataset(input_ids=validation_input_ids, attention_masks=validation_attention_mask, labels=validation_labels)
validation_dataloader = torch.utils.data.DataLoader(validation_dataset, batch_size=8, drop_last=False)

Train the model:
optimizer = torch.optim.AdamW(model.parameters(), lr=.01)
cross_entropy = nn.CrossEntropyLoss().to(device)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=.1, patience=2, verbose=True)

model.to(device)
def big_training_loop(epochs, optim, clf, lossf, train_loader, val_loader, accuracy_score):
    # variables for early stopping
    n_epochs_stop = 10
    epochs_no_improve = 0
    min_val_loss = float('inf')

    # running epoch
    for epoch in range(1, epochs + 1):

        # variables for performance monitoring
        loss_train_list = []
        loss_val_list = []
        correct_train_list_fscroe = []
        correct_train_list = []
        correct_val_list_fscroe = []
        correct_val_list = []

        # training on batches
        for (text, mask, labels), (val_text, val_mask, val_labels) in zip(train_loader, val_loader):
            # - training section - #
            clf.train()
            text, mask, labels = text.to(device), mask.to(device), labels.to(device)
            output = model(text, token_type_ids=None, attention_mask=mask)[0]
            # compute loss function
            loss = lossf(output, labels)
            # append loss output to loss_train_list for mnitoring
            loss_train_list.append(loss.item())

            # convert tensors to numpy array
            labels = labels.cpu().detach().numpy()
            output = torch.argmax(output, dim=1).cpu().detach().numpy()
            # calculate accuracy score
            correct_train_list_fscroe.append(f1_score(output, labels, average='macro'))
            correct_train_list.append(accuracy_score(output, labels))
            # compute back propagation
            loss.backward()
            # update weights
            optim.zero_grad()
            torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
            optim.step()
            # clean gradients to not accumulate

            # - evaluation section - #
            clf.eval()
            with torch.no_grad():
                # - training section - #
                val_text, val_mask, val_labels = val_text.to(device), val_mask.to(device), val_labels.to(device)
                val_output = model(val_text, token_type_ids=None, attention_mask=val_mask)[0]
                # compute loss function
                val_loss = lossf(val_output, val_labels)
                # append loss output to loss_val_list for mnitoring
                loss_val_list.append(val_loss.item())

                # convert tensors to numpy array
                val_output = torch.argmax(val_output, dim=1).cpu().detach().numpy()
                val_labels = val_labels.cpu().detach().numpy()

                # calculate accuracy score
                correct_val_list.append(accuracy_score(val_output, val_labels))
                correct_val_list_fscroe.append(f1_score(val_output, val_labels, average='macro'))

            # change lr if the loss didn't decrease

            loss = torch.mean(torch.FloatTensor(loss_train_list))
            val_loss = torch.mean(torch.FloatTensor(loss_val_list))
            acc = torch.mean(torch.FloatTensor(correct_train_list)) * 100
            fscore = torch.mean(torch.FloatTensor(correct_train_list_fscroe)) * 100
            val_acc = torch.mean(torch.FloatTensor(correct_val_list)) * 100
            val_fscore = torch.mean(torch.FloatTensor(correct_val_list_fscroe)) * 100

        scheduler.step(val_loss)

        # save best model if the loss is the best
        if torch.mean(torch.FloatTensor(loss_val_list)) < min_val_loss:
            # variables for best performance
            best_epoch = epoch
            best_loss = loss
            best_val_loss = val_loss
            best_acc = acc
            best_fscore = fscore
            best_val_acc = val_acc
            best_val_fscore = val_fscore
            # save best model
            epochs_no_improve = 0
            min_val_loss = best_val_loss
            # print the current epoch as the best epoch

            print(
                f'BEST EPOCH: Epoch({epoch}) -> Train: (Accuracy: {best_acc:.1f}, f-score: {best_fscore:.1f}, Loss: {best_loss:.4f}) | Val: (Accuracy: {best_val_acc:.1f}, f-score: {best_val_fscore:.1f}, Loss: {best_val_loss:.4f})')

        else:
            # print the current epoch as normal epoch
            print(
                f'Epoch({epoch}) -> Train: (Accuracy: {acc:.1f}, f-score: {fscore:.1f}, Loss: {loss:.4f}) | Val: (Accuracy: {val_acc:.1f}, f-score: {val_fscore:.1f},Loss: {val_loss:.4f})')
            # if epochs_no_improve reached n_epochs_stop the training will stop
            epochs_no_improve += 1

        # early stop the training
        if epoch > 5 and epochs_no_improve == n_epochs_stop:
            torch.save(clf,
                       f'clf_val_loss_{best_val_loss:.4f}_f-score_{best_val_fscore:.1f}_val_acc_{best_val_acc:.1f}.pt')
            print('Early stopping!')
            print()
            print(
                f'BEST EPOCH: Epoch({best_epoch}) -> Train: (Accuracy: {best_acc:.1f}, f-score: {best_fscore:.1f}, Loss: {best_loss:.4f}) | Val: (Accuracy: {best_val_acc:.1f}, f-score: {best_val_fscore:.1f}, Loss: {best_val_loss:.4f})')
            break


print('Running...')
big_training_loop(555, optimizer, model, cross_entropy, dataloader, validation_dataloader, accuracy_score=accuracy_score)

Running...
BEST EPOCH: Epoch(1) -> Train: (Accuracy: 66.0, f-score: 42.7, Loss: 0.6451) | Val: (Accuracy: 65.3, f-score: 45.0, Loss: 0.6572)
BEST EPOCH: Epoch(2) -> Train: (Accuracy: 66.0, f-score: 42.7, Loss: 0.6510) | Val: (Accuracy: 65.3, f-score: 45.0, Loss: 0.6567)
BEST EPOCH: Epoch(3) -> Train: (Accuracy: 66.0, f-score: 42.7, Loss: 0.6487) | Val: (Accuracy: 65.3, f-score: 45.0, Loss: 0.6563)
BEST EPOCH: Epoch(4) -> Train: (Accuracy: 66.0, f-score: 42.7, Loss: 0.6472) | Val: (Accuracy: 65.3, f-score: 45.0, Loss: 0.6559)
BEST EPOCH: Epoch(5) -> Train: (Accuracy: 66.0, f-score: 42.7, Loss: 0.6471) | Val: (Accuracy: 65.3, f-score: 45.0, Loss: 0.6555)
BEST EPOCH: Epoch(6) -> Train: (Accuracy: 66.0, f-score: 42.7, Loss: 0.6519) | Val: (Accuracy: 65.3, f-score: 45.0, Loss: 0.6551)
BEST EPOCH: Epoch(7) -> Train: (Accuracy: 66.0, f-score: 42.7, Loss: 0.6591) | Val: (Accuracy: 65.3, f-score: 45.0, Loss: 0.6548)