from datasets import load_dataset from transformers import BertForSequenceClassification, Trainer, TrainingArguments from transformers import BertTokenizer # Load the dataset dataset = load_dataset('csv', data_files='dataset.csv') # Load the tokenizer tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') # Tokenize the dataset def tokenize_function(examples): return tokenizer(examples['question'], padding="max_length", truncation=True) tokenized_datasets = dataset.map(tokenize_function, batched=True) # Load the model model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=1) # Define training arguments training_args = TrainingArguments( output_dir="./results", evaluation_strategy="epoch", learning_rate=2e-3, per_device_train_batch_size=8, per_device_eval_batch_size=8, num_train_epochs=10, weight_decay=0.01, ) # Create Trainer instance trainer = Trainer( model=model, args=training_args, train_dataset=tokenized_datasets['train'], eval_dataset=tokenized_datasets['test'] ) # Train the model trainer.train()