forked from KyleBenzle/SynerGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfine_tune_bert.py
More file actions
39 lines (31 loc) · 1.14 KB
/
fine_tune_bert.py
File metadata and controls
39 lines (31 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# fine_tune_bert.py
from transformers import BertTokenizer, BertForSequenceClassification, Trainer, TrainingArguments
from datasets import load_dataset
# Assuming you have preprocessed ChatLog.txt into a suitable format and loaded it as a dataset
dataset = load_dataset('csv', data_files={'train': 'chat_data.csv'})
# Initialize the tokenizer and model for Bert
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
# Tokenize the input text
def tokenize_function(examples):
return tokenizer(examples["text"], padding="max_length", truncation=True)
tokenized_datasets = dataset.map(tokenize_function, batched=True)
# Define training arguments
training_args = TrainingArguments(
output_dir="./results",
num_train_epochs=3,
per_device_train_batch_size=8,
warmup_steps=500,
weight_decay=0.01,
logging_dir="./logs",
)
# Initialize Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_datasets["train"],
)
# Train the model
trainer.train()
# Save the fine-tuned model
model.save_pretrained('./fine_tuned_model')