from datasets import load_dataset
def preprocess_for_question_generation(examples):
    if examples["answer"] is None or examples["context"] is None:
        return None
​
    if not isinstance(examples["answer"], list) or not isinstance(examples["context"], list):
        return None
​
    if len(examples["answer"]) != len(examples["context"]):
        return None
​
    prepared_questions = []
    prepared_input_texts = []
    for context, answer in zip(examples["context"], examples["answer"]):
        input_format = f"generate question: context: {context} answer: {answer}"
        prepared_input_texts.append(input_format)
    
    tokenized_inputs = tokenizer(
        prepared_input_texts,
        max_length=128,
        padding="max_length",
        truncation=True,
        return_tensors="pt"
    )
​
    if "question" in examples: 
        tokenized_questions = tokenizer(
            examples["question"],
            max_length=64,
            padding="max_length",
            truncation=True,
            return_tensors="pt"
        )
        tokenized_inputs["labels"] = tokenized_questions["input_ids"].clone()
         # Convert tensors to NumPy arrays
    tokenized_inputs_numpy = {}
    for key, value in tokenized_inputs.items():
        tokenized_inputs_numpy[key] = value.numpy()
​
    return tokenized_inputs_numpy
​
# Apply preprocessing to datasets
tokenized_train_dataset = train_dataset.map(preprocess_for_question_generation, batched=True)
tokenized_val_dataset = val_dataset.map(preprocess_for_question_generation, batched=True)
tokenized_test_dataset = test_dataset.map(preprocess_for_question_generation, batched=True)
100%
43/43 [00:20<00:00, 2.28ba/s]
100%
11/11 [00:05<00:00, 2.40ba/s]
100%
6/6 [00:02<00:00, 2.33ba/s]
add Codeadd Markdown
# Define data collator for sequence-to-sequence models
data_collator = DataCollatorForSeq2Seq(tokenizer)
add Codeadd Markdown
# Define the training arguments
training_args = TrainingArguments(
    output_dir="./question_generation_arat1024_3_model",
    num_train_epochs=3,
    per_device_train_batch_size=8,
    per_device_eval_batch_size=8,
    save_total_limit=2,
    evaluation_strategy="epoch",
    report_to=[],  # Set report_to to an empty list to disable wandb logging
    learning_rate=5e-5  # Set your desired learning rate here
)
# Model trainer
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_train_dataset,
    eval_dataset=tokenized_val_dataset,
    tokenizer=tokenizer,
    data_collator=data_collator,
    #optimizers=(optimizer, scheduler),
    # Add the following line to specify decoder_input_ids or decoder_inputs_embeds
    #decoder_input_ids=torch.tensor([[tokenizer.pad_token_id]*128]),  # Example, adjust as needed
)
​
# Train the model
trainer.train()
 [16092/16092 1:42:41, Epoch 3/3]
Epoch	Training Loss	Validation Loss
1	0.594300	0.511139
2	0.529000	0.493306
3	0.501300	0.489161
TrainOutput(global_step=16092, training_loss=0.6901373387923917, metrics={'train_runtime': 6162.841, 'train_samples_per_second': 20.889, 'train_steps_per_second': 2.611, 'total_flos': 2.796696858997555e+16, 'train_loss': 0.6901373387923917, 'epoch': 3.0})

after i train how can compute blue and add function generate question