tekkonetes
commited on
Commit
·
af85fa9
1
Parent(s):
2ad999c
Create finetune.py
Browse files- finetune.py +27 -0
finetune.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
mport torch
|
2 |
+
from transformers import GPTNeoForCausalLM, GPT2Tokenizer
|
3 |
+
|
4 |
+
# Load the pre-trained GPT-Neo model tokenizer (Replace with smaller or larger version)
|
5 |
+
model = GPTNeoForCausalLM.from_pretrained('EleutherAI/gpt-neo-1.3B')
|
6 |
+
tokenizer = GPT2Tokenizer.from_pretrained('EleutherAI/gpt-neo-1.3B')
|
7 |
+
|
8 |
+
# Load the text dataset
|
9 |
+
with open('text.txt', 'r') as f:
|
10 |
+
text = f.read()
|
11 |
+
|
12 |
+
# Tokenize the text dataset
|
13 |
+
encoding = tokenizer.encode(text, return_tensors='pt')
|
14 |
+
|
15 |
+
# Fine-tune the model on the text dataset
|
16 |
+
model.train()
|
17 |
+
optimizer = torch.optim.Adam(model.parameters(), lr=5e-5)
|
18 |
+
for i in range(100):
|
19 |
+
loss = model(encoding, labels=encoding)[0]
|
20 |
+
loss.backward()
|
21 |
+
optimizer.step()
|
22 |
+
optimizer.zero_grad()
|
23 |
+
print(f'Epoch {i+1}, Loss: {loss.item()}')
|
24 |
+
|
25 |
+
# Save the fine-tuned model and tokenizer
|
26 |
+
model.save_pretrained('fine-tuned-gpt-neo')
|
27 |
+
tokenizer.save_pretrained('fine-tuned-gpt-neo')
|