dnnsdunca commited on
Commit
8a12533
1 Parent(s): 71f3e45

Create notebooks/fine_tune.ipynb

Browse files
Files changed (1) hide show
  1. notebooks/fine_tune.ipynb +43 -0
notebooks/fine_tune.ipynb ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Fine-tuning example notebook using Hugging Face's transformers and datasets libraries.
2
+ import json
3
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer, Trainer, TrainingArguments
4
+ from datasets import load_dataset
5
+
6
+ # Load configuration
7
+ with open('../config/config.json') as f:
8
+ config = json.load(f)
9
+
10
+ # Load dataset
11
+ dataset = load_dataset('csv', data_files={'train': '../data/train.csv', 'validation': '../data/valid.csv'})
12
+
13
+ # Load model and tokenizer
14
+ model = AutoModelForSequenceClassification.from_pretrained(config['model_name'], num_labels=config['num_labels'])
15
+ tokenizer = AutoTokenizer.from_pretrained(config['model_name'])
16
+
17
+ # Tokenize dataset
18
+ def tokenize_function(examples):
19
+ return tokenizer(examples['text'], padding="max_length", truncation=True)
20
+
21
+ tokenized_datasets = dataset.map(tokenize_function, batched=True)
22
+
23
+ # Training arguments
24
+ training_args = TrainingArguments(
25
+ output_dir='../results',
26
+ learning_rate=config['learning_rate'],
27
+ per_device_train_batch_size=config['batch_size'],
28
+ num_train_epochs=config['num_epochs'],
29
+ evaluation_strategy="epoch",
30
+ save_strategy="epoch",
31
+ logging_dir='../logs'
32
+ )
33
+
34
+ trainer = Trainer(
35
+ model=model,
36
+ args=training_args,
37
+ train_dataset=tokenized_datasets['train'],
38
+ eval_dataset=tokenized_datasets['validation'],
39
+ tokenizer=tokenizer
40
+ )
41
+
42
+ trainer.train()
43
+ trainer.save_model('../model')