bachiembmt commited on
Commit
8747974
1 Parent(s): c2db305

Create README.py

Browse files
Files changed (1) hide show
  1. README.py +55 -0
README.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from transformers import GPT2Tokenizer, GPT2LMHeadModel
4
+ from datasets import load_dataset
5
+ from torch.utils.data import DataLoader
6
+
7
+ # Load the GPT-2 tokenizer and model
8
+ tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
9
+ model = GPT2LMHeadModel.from_pretrained("gpt2")
10
+
11
+ # Define the loss function and optimizer
12
+ criterion = nn.CrossEntropyLoss()
13
+ optimizer = torch.optim.Adam(model.parameters())
14
+
15
+ # Get the training dataset from Hugging Face
16
+ dataset = load_dataset("wikitext", 'wikitext-103-v1')
17
+
18
+ # Define the number of training steps
19
+ num_steps = 1000
20
+
21
+ # Define the data loader
22
+ data_loader = DataLoader(dataset, batch_size=32, shuffle=True)
23
+
24
+ # Training loop
25
+ for step in range(num_steps):
26
+
27
+ # Get the next batch of data
28
+ input_ids = dataset['train'][step]['text']
29
+ labels = dataset['train'][step]['text']
30
+
31
+ if not input_ids or not labels:
32
+ continue
33
+
34
+ input_ids = tokenizer.encode(input_ids, return_tensors='pt').unsqueeze(0)
35
+ labels = tokenizer.encode(labels, return_tensors='pt').unsqueeze(0)
36
+
37
+
38
+ # Forward pass
39
+ outputs = model(input_ids)
40
+ logits = outputs[0]
41
+
42
+ # Compute the loss
43
+ loss = criterion(logits.view(-1, logits.size(-1)), labels.view(-1))
44
+
45
+ # Backward pass and optimization
46
+ optimizer.zero_grad()
47
+ loss.backward()
48
+ optimizer.step()
49
+
50
+ # Print the current loss
51
+ if step % 100 == 0:
52
+ print(f'Step {step}, Loss {loss.item()}')
53
+
54
+ # Save the fine-tuned model
55
+ torch.save(model.state_dict(), 'fine_tuned_gpt2.pth')