I am getting an error on the final line: output = model(text, labels).
It works perfectly fine with I use:
self.encoder = BertForSequenceClassification.from_pretrained(options_name)
Can anyone help me figure out the source of the error? Thanks in advance!
source_folder = './Data/test'
destination_folder = './Model'

# Step 1: Importing Libraries
import matplotlib.pyplot as plt
import pandas as pd
import torch
# Preliminaries
from torchtext.data import Field, TabularDataset, BucketIterator, Iterator
# Models
import torch.nn as nn
from transformers import BertTokenizer, BertForSequenceClassification, BertForTokenClassification
# Training
import torch.optim as optim
# Evaluation
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
import seaborn as sns
import random 
import numpy as np
import sys
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print(device)

# For reproducibility
seed = 2021
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)

# Step 2: Preprocess and Prepare Dataset
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

# Model parameter
# MAX_SEQ_LEN = 128
PAD_INDEX = tokenizer.convert_tokens_to_ids(tokenizer.pad_token)
UNK_INDEX = tokenizer.convert_tokens_to_ids(tokenizer.unk_token)

# Fields

label_field = Field(sequential=False, use_vocab=False, batch_first=True, dtype=torch.float)
text_field = Field(use_vocab=False, tokenize=tokenizer.encode, lower=False, include_lengths=False, batch_first=True,
                  fix_length=None, pad_token=PAD_INDEX, unk_token=UNK_INDEX)
fields = [('label', label_field), ('text', text_field)]

# TabularDataset

train, valid, test = TabularDataset.splits(path=source_folder, train='train.csv', validation='valid.csv',
                                          test='test.csv', format='CSV', fields=fields, skip_header=True)
# Iterators

train_iter = BucketIterator(train, batch_size=16, sort_key=lambda x: len(x.text),
                           device=device, train=True, sort=True, sort_within_batch=True)
valid_iter = BucketIterator(valid, batch_size=16, sort_key=lambda x: len(x.text),
                           device=device, train=True, sort=True, sort_within_batch=True)
test_iter = Iterator(test, batch_size=16, device=device, train=False, shuffle=False, sort=False)

# Step 3: Build Model
class BERT(nn.Module):
   def __init__(self):
       super(BERT, self).__init__()

       options_name = "bert-base-uncased"
       self.encoder = BertForTokenClassification.from_pretrained(options_name)
      #self.encoder = BertForSequenceClassification.from_pretrained(options_name)

   def forward(self, text, label):
       loss, text_fea = self.encoder(text, labels=label)[:2]

       return loss, text_fea

# Step 4: Training
# Save and Load Functions
def save_checkpoint(save_path, model, valid_loss):

   if save_path == None:
       return
   
   state_dict = {'model_state_dict': model.state_dict(),
                 'valid_loss': valid_loss}
   
   torch.save(state_dict, save_path)
   print(f'Model saved to ==> {save_path}')

def load_checkpoint(load_path, model):
   
   if load_path==None:
       return
   
   state_dict = torch.load(load_path, map_location=device)
   print(f'Model loaded from <== {load_path}')
   
   model.load_state_dict(state_dict['model_state_dict'])
   return state_dict['valid_loss']


def save_metrics(save_path, train_loss_list, valid_loss_list, global_steps_list):

   if save_path == None:
       return
   
   state_dict = {'train_loss_list': train_loss_list,
                 'valid_loss_list': valid_loss_list,
                 'global_steps_list': global_steps_list}
   
   torch.save(state_dict, save_path)
   print(f'Model saved to ==> {save_path}')


def load_metrics(load_path):

   if load_path==None:
       return
   
   state_dict = torch.load(load_path, map_location=device)
   print(f'Model loaded from <== {load_path}')
   
   return state_dict['train_loss_list'], state_dict['valid_loss_list'], state_dict['global_steps_list']

# Training Function

def train(model,
         optimizer,
         criterion = nn.BCELoss(),
         train_loader = train_iter,
         valid_loader = valid_iter,
         num_epochs = 5,
         eval_every = len(train_iter) // 2,
         file_path = destination_folder,
         best_valid_loss = float("Inf")):
   
   # initialize running values
   running_loss = 0.0
   valid_running_loss = 0.0
   global_step = 0
   train_loss_list = []
   valid_loss_list = []
   global_steps_list = []

#     inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
# labels = torch.tensor([1] * inputs["input_ids"].size(1)).unsqueeze(0)  # Batch size 1

   # training loop
   model.train()
   for epoch in range(num_epochs):
       for (labels, text), _ in train_loader:
           labels = labels.type(torch.LongTensor)           
           labels = labels.to(device)
           
           text = text.type(torch.LongTensor)  
           text = text.to(device)
           
           output = model(text, labels)