Repository navigation
Expand file tree
/
Copy pathLLM_fine_tune.py
More file actions
119 lines (101 loc) · 3.36 KB
/
Copy pathLLM_fine_tune.py
File metadata and controls
119 lines (101 loc) · 3.36 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# pip install transformers torch pandas scikit-learn
import json
import pandas as pd
from sklearn.model_selection import train_test_split
from transformers import (
AutoTokenizer,
AutoModelForCausalLM,
LineByLineTextDataset,
TrainingArguments,
Trainer,
DataCollatorForLanguageModeling
)
# Define the model name you want to fine-tune
model_name = "ncfrey/ChemGPT-4.7M"
# Load the pre-trained model and tokenizer
print("Loading pre-trained model and tokenizer...")
try:
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
model.resize_token_embeddings(len(tokenizer)) # Important to resize the model's embeddings
except Exception as e:
# print(f"Error loading model or tokenizer: {e} ")
exit()
# Read and split SELFIES data from the CSV file ---
csv_file_path = "All_ligands.csv"
selfies_column = "Ligand_Selfies"
print(f"Reading and splitting SELFIES data from '{csv_file_path}'...")
try:
df = pd.read_csv(csv_file_path)
selfies_data = df[selfies_column].tolist()
if not selfies_data:
print("Error: The column 'Ligand_Selfies' is empty or contains no valid data.")
exit()
except FileNotFoundError:
print(f"Error: The file '{csv_file_path}' was not found.")
exit()
except KeyError:
print(f"Error: The column '{selfies_column}' was not found in the CSV file.")
exit()
# Split the data into a training set and a validation set
train_selfies, val_selfies = train_test_split(selfies_data, test_size=0.1, random_state=42)
# Prepare the datasets and data collator
train_txt_file = "train_selfies_data.txt"
val_txt_file = "val_selfies_data.txt"
print("Saving training and validation data to temporary text files...")
with open(train_txt_file, 'w') as f:
for selfies in train_selfies:
f.write(f"{selfies}\n")
with open(val_txt_file, 'w') as f:
for selfies in val_selfies:
f.write(f"{selfies}\n")
# Load datasets
train_dataset = LineByLineTextDataset(
tokenizer=tokenizer,
file_path=train_txt_file,
block_size=128
)
eval_dataset = LineByLineTextDataset(
tokenizer=tokenizer,
file_path=val_txt_file,
block_size=128
)
data_collator = DataCollatorForLanguageModeling(
tokenizer=tokenizer,
mlm=False #ignores padding tokens
)
# Define training parameters and fine-tune the model
training_args = TrainingArguments(
output_dir="./chemgpt_finetuned",
overwrite_output_dir=True,
num_train_epochs=500,
per_device_train_batch_size=40,
save_steps=1000,
save_total_limit=3,
eval_strategy="steps", #evaluate performance after "steps"
eval_steps=500,
logging_steps=5
)
trainer = Trainer(
model=model,
args=training_args,
data_collator=data_collator,
train_dataset=train_dataset,
eval_dataset=eval_dataset
)
print("Starting fine-tuning process...")
# Capture the training output
training_output = trainer.train()
# Save the fine-tuned model and the log history
output_directory = "./chemgpt_finetuned"
print(f"\nFine-tuning complete. Saving model and tokenizer to '{output_directory}'...")
trainer.save_model(output_directory)
tokenizer.save_pretrained(output_directory)
# Explicitly save the log history to a JSON file
log_file_path = "training_log.json"
print(f"Saving loss information to '{log_file_path}'...")
with open(log_file_path, 'w') as f:
json.dump(trainer.state.log_history, f, indent=4)
print("Process finished successfully.")