Model did not return a loss / BertForQuestionAnswering.forward() got an unexpected keyword argument 'labels'

Hello, I have this dataset :

{"version": "0.1.0",
    "data":
 [
        {"id": "hi", 
        "question": ["hi", "how are you"],
        "answers": ["hi!", "how can i help you?"],
        "context": ""
        },
        
        {"id": "bye", 
        "question": ["Bye", "good bye", "see you"],
        "answers": ["see you later", "have a nice day", "bye", "thanks for visiting"],
        "context": ""
        },

        {"id": "weather", 
        "question": ["how is the weather", "weather forecast", "weather"],
        "answers": ["weather is good", "we have 25 degrees"],
        "context": ""
        }

    ]
}

and I am trying to build a question answer bot.

I am using this code:

train = load_dataset('json', data_files='intents.json', field='data', split='train[:80%]')
test = load_dataset('json', data_files='intents.json', field='data', split='train[80%:]')
 
data = datasets.DatasetDict({"train":train, "test": test})

tokenized = data.map(preprocess_func, batched=True)

#data_collator = DefaultDataCollator()
data_collator = DataCollatorForLanguageModeling(
    tokenizer=tokenizer, mlm=True
)

device = "cpu"
model = AutoModelForQuestionAnswering.from_pretrained('bert-base-uncased')
model = model.to(device)

training_args = TrainingArguments(
  output_dir="./results",
  evaluation_strategy="epoch",
  learning_rate=2e-5,
  per_device_train_batch_size=2,
  num_train_epochs=2,
  weight_decay=0.01,
)

trainer = Trainer(
  model=model,
  args=training_args,
  train_dataset=tokenized["train"],
  tokenizer=tokenizer,
  data_collator=data_collator,
)

trainer.train()

and I am receiving:

BertForQuestionAnswering.forward() got an unexpected keyword argument 'labels'

but I don’t have any labels in the data:

tokenized

DatasetDict({
    train: Dataset({
        features: ['context', 'id', 'question', 'answers', 'input_ids', 'token_type_ids', 'attention_mask'],
        num_rows: 2
    })
    test: Dataset({
        features: ['context', 'id', 'question', 'answers', 'input_ids', 'token_type_ids', 'attention_mask'],
        num_rows: 1
    })
})

If I use :

DefaultDataCollator() instead of DataCollatorForLanguageModeling, I receive:

The model did not return a loss from the inputs, only the following keys: start_logits,end_logits

I am not sure if the preprocess_func needs more things to do.

Like this:

def preprocess_function(examples):
    questions = [q.strip() for q in examples["question"]]
    inputs = tokenizer(
        questions,
        examples["context"],
        max_length=512,
        truncation="only_second",
        return_offsets_mapping=True,
        padding="max_length",
    )

    offset_mapping = inputs.pop("offset_mapping")
    answers = examples["answers"]
    start_positions = []
    end_positions = []

    for i, offset in enumerate(offset_mapping):
        answer = answers[i]
        start_char = answer["answer_start"][0]
        end_char = answer["answer_start"][0] + len(answer["text"][0])
        sequence_ids = inputs.sequence_ids(i)

        # Find the start and end of the context
        idx = 0
        while sequence_ids[idx] != 1:
            idx += 1
        context_start = idx
        while sequence_ids[idx] == 1:
            idx += 1
        context_end = idx - 1

        # If the answer is not fully inside the context, label it (0, 0)
        if offset[context_start][0] > end_char or offset[context_end][1] < start_char:
            start_positions.append(0)
            end_positions.append(0)
        else:
            # Otherwise it's the start and end token positions
            idx = context_start
            while idx <= context_end and offset[idx][0] <= start_char:
                idx += 1
            start_positions.append(idx - 1)

            idx = context_end
            while idx >= context_start and offset[idx][1] >= end_char:
                idx -= 1
            end_positions.append(idx + 1)

    inputs["start_positions"] = start_positions
    inputs["end_positions"] = end_positions
    return inputs