Update README.md
Browse files
README.md
CHANGED
|
@@ -75,6 +75,113 @@ print(output[0]['generated_text'])
|
|
| 75 |
|
| 76 |
```
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
### Training hyperparameters
|
| 79 |
|
| 80 |
The following hyperparameters were used during training:
|
|
|
|
| 75 |
|
| 76 |
```
|
| 77 |
|
| 78 |
+
### Outputs
|
| 79 |
+
microsoft/Phi-3-medium-128k-instruct
|
| 80 |
+
```python
|
| 81 |
+
To train a language model using Lora and SFT (Supervised Fine-tuning), you can follow these steps:
|
| 82 |
+
|
| 83 |
+
1. Install the required libraries:
|
| 84 |
+
|
| 85 |
+
```python
|
| 86 |
+
!pip install transformers
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
2. Import the necessary libraries:
|
| 90 |
+
|
| 91 |
+
```python
|
| 92 |
+
import torch
|
| 93 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, Trainer, TrainingArguments
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
3. Load the pre-trained Lora model and tokenizer:
|
| 97 |
+
|
| 98 |
+
```python
|
| 99 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/llama-65b-lora")
|
| 100 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/llama-65b-lora")
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
4. Load the SFT dataset:
|
| 104 |
+
|
| 105 |
+
```python
|
| 106 |
+
# Replace with the path to your SFT dataset
|
| 107 |
+
train_dataset =...
|
| 108 |
+
```
|
| 109 |
+
|
| 110 |
+
5. Define the training arguments:
|
| 111 |
+
|
| 112 |
+
```python
|
| 113 |
+
training_args = TrainingArguments(
|
| 114 |
+
output_dir="./results",
|
| 115 |
+
num_train_epochs=3,
|
| 116 |
+
per_device_train_batch_size=4,
|
| 117 |
+
per_device_eval_batch_size=4,
|
| 118 |
+
warmup_steps=500,
|
| 119 |
+
weight_decay=0.01,
|
| 120 |
+
logging_dir='./logs',
|
| 121 |
+
logging_steps=10,
|
| 122 |
+
)
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
6. Create a custom Trainer for Lora and SFT:
|
| 126 |
+
|
| 127 |
+
```python
|
| 128 |
+
class LoraSFTTrainer(Trainer):
|
| 129 |
+
def compute_loss(self, model, inputs):
|
| 130 |
+
labels = inputs.pop("labels")
|
| 131 |
+
outputs = model(**inputs, use_cache=False)
|
| 132 |
+
lm_logits = outputs.logits
|
| 133 |
+
loss_fct = torch.nn.CrossEntropyLoss()
|
| 134 |
+
masked_lm_loss = loss_fct(lm_logits.view(-1, self.model.config.vocab_size), labels.view(-1))
|
| 135 |
+
return masked_lm_loss
|
| 136 |
+
```
|
| 137 |
+
|
| 138 |
+
7. Initialize the trainer and train the model:
|
| 139 |
+
|
| 140 |
+
```python
|
| 141 |
+
trainer = LoraSFTTrainer(
|
| 142 |
+
model=model,
|
| 143 |
+
args=training_args,
|
| 144 |
+
train_dataset=train_dataset,
|
| 145 |
+
eval_dataset=None,
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
trainer.train()
|
| 149 |
+
```
|
| 150 |
+
|
| 151 |
+
This code will train the Lora model using the SFT dataset. You can adjust the training arguments and the dataset path according to your needs.
|
| 152 |
+
|
| 153 |
+
```
|
| 154 |
+
REILX/Phi-3-medium-128k-code-instruct
|
| 155 |
+
```python
|
| 156 |
+
import torch
|
| 157 |
+
from transformers import RobertaForCausalLM, RobertaTokenizer
|
| 158 |
+
|
| 159 |
+
# Load the model and tokenizer
|
| 160 |
+
model = RobertaForCausalLM.from_pretrained('roberta-base')
|
| 161 |
+
tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
|
| 162 |
+
|
| 163 |
+
# Load the data
|
| 164 |
+
data = [
|
| 165 |
+
"This is a sample sentence.",
|
| 166 |
+
"Another sample sentence."
|
| 167 |
+
]
|
| 168 |
+
|
| 169 |
+
# Tokenize the data
|
| 170 |
+
input_ids = [tokenizer.encode(sentence, add_special_tokens=True) for sentence in data]
|
| 171 |
+
|
| 172 |
+
# Train the model
|
| 173 |
+
model.train()
|
| 174 |
+
for input_id in input_ids:
|
| 175 |
+
outputs = model(input_id, labels=input_id)
|
| 176 |
+
loss = outputs.loss
|
| 177 |
+
loss.backward()
|
| 178 |
+
optimizer.step()
|
| 179 |
+
|
| 180 |
+
# Save the model
|
| 181 |
+
model.save_pretrained('my_model')
|
| 182 |
+
|
| 183 |
+
```
|
| 184 |
+
|
| 185 |
### Training hyperparameters
|
| 186 |
|
| 187 |
The following hyperparameters were used during training:
|