Upload finetune.ipynb
Browse files- finetune.ipynb +23 -7
finetune.ipynb
CHANGED
|
@@ -143,7 +143,13 @@
|
|
| 143 |
"name": "stdout",
|
| 144 |
"output_type": "stream",
|
| 145 |
"text": [
|
| 146 |
-
"Epoch 1/7, Train
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
]
|
| 148 |
}
|
| 149 |
],
|
|
@@ -183,8 +189,18 @@
|
|
| 183 |
"\n",
|
| 184 |
" print(f'\\rEpoch {epoch+1}/{num_epochs}, Train Loss: {train_loss/len(train_loader)}, Valid Loss: {valid_loss/len(valid_loader)}, lr: {learning_rate}',end=\"\\n\")\n",
|
| 185 |
" learning_rate /= 5\n",
|
| 186 |
-
"model.save_pretrained('
|
| 187 |
-
"tokenizer.save_pretrained('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
]
|
| 189 |
},
|
| 190 |
{
|
|
@@ -218,9 +234,9 @@
|
|
| 218 |
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
|
| 219 |
"\n",
|
| 220 |
"# Load the fine-tuned model\n",
|
| 221 |
-
"model_path = '
|
| 222 |
-
"tokenizer = GPT2Tokenizer.from_pretrained(
|
| 223 |
-
"model = GPT2LMHeadModel.from_pretrained(
|
| 224 |
"\n",
|
| 225 |
"# Set the pad_token_id to the same value as the unk_token_id\n",
|
| 226 |
"#model.config.pad_token_id = tokenizer.unk_token_id\n",
|
|
@@ -232,7 +248,7 @@
|
|
| 232 |
"temperature = 1.0\n",
|
| 233 |
"\n",
|
| 234 |
"# Generate text using beam search, n-grams, and other techniques\n",
|
| 235 |
-
"prompt = \"
|
| 236 |
"\n",
|
| 237 |
"def generate(prompt):\n",
|
| 238 |
" input_ids = tokenizer.encode(prompt, return_tensors='pt').to(device)\n",
|
|
|
|
| 143 |
"name": "stdout",
|
| 144 |
"output_type": "stream",
|
| 145 |
"text": [
|
| 146 |
+
"Epoch 1/7, Train Loss: 0.5704409927129745, Valid Loss: 0.3897556330009205, lr: 0.0005\n",
|
| 147 |
+
"Epoch 2/7, Train Loss: 0.403159585849541, Valid Loss: 0.24347291268953464, lr: 0.0001\n",
|
| 148 |
+
"Epoch 3/7, Train Loss: 0.27275778398644634, Valid Loss: 0.13453135721203757, lr: 2e-05\n",
|
| 149 |
+
"Epoch 4/7, Train Loss: 0.1717792261482739, Valid Loss: 0.07003633981775038, lr: 4.000000000000001e-06\n",
|
| 150 |
+
"Epoch 5/7, Train Loss: 0.10565993448764813, Valid Loss: 0.03993069042065522, lr: 8.000000000000002e-07\n",
|
| 151 |
+
"Epoch 6/7, Train Loss: 0.0703794076675322, Valid Loss: 0.02531332855408148, lr: 1.6000000000000003e-07\n",
|
| 152 |
+
"Epoch 7/7, Train Batch 7/82, Train Loss: 0.0036747022645502556"
|
| 153 |
]
|
| 154 |
}
|
| 155 |
],
|
|
|
|
| 189 |
"\n",
|
| 190 |
" print(f'\\rEpoch {epoch+1}/{num_epochs}, Train Loss: {train_loss/len(train_loader)}, Valid Loss: {valid_loss/len(valid_loader)}, lr: {learning_rate}',end=\"\\n\")\n",
|
| 191 |
" learning_rate /= 5\n",
|
| 192 |
+
"model.save_pretrained('brunosan/GPT2-impactscience')\n",
|
| 193 |
+
"tokenizer.save_pretrained('brunosan/GPT2-impactscience')"
|
| 194 |
+
]
|
| 195 |
+
},
|
| 196 |
+
{
|
| 197 |
+
"cell_type": "code",
|
| 198 |
+
"execution_count": null,
|
| 199 |
+
"metadata": {},
|
| 200 |
+
"outputs": [],
|
| 201 |
+
"source": [
|
| 202 |
+
"model.save_pretrained('brunosan/GPT2-impactscience')\n",
|
| 203 |
+
"tokenizer.save_pretrained('brunosan/GPT2-impactscience')"
|
| 204 |
]
|
| 205 |
},
|
| 206 |
{
|
|
|
|
| 234 |
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
|
| 235 |
"\n",
|
| 236 |
"# Load the fine-tuned model\n",
|
| 237 |
+
"model_path = 'brunosan/GPT2-impactscience'\n",
|
| 238 |
+
"tokenizer = GPT2Tokenizer.from_pretrained(model_path)\n",
|
| 239 |
+
"model = GPT2LMHeadModel.from_pretrained(model_path).to(device)\n",
|
| 240 |
"\n",
|
| 241 |
"# Set the pad_token_id to the same value as the unk_token_id\n",
|
| 242 |
"#model.config.pad_token_id = tokenizer.unk_token_id\n",
|
|
|
|
| 248 |
"temperature = 1.0\n",
|
| 249 |
"\n",
|
| 250 |
"# Generate text using beam search, n-grams, and other techniques\n",
|
| 251 |
+
"prompt = \"The impact of climate change on \"\n",
|
| 252 |
"\n",
|
| 253 |
"def generate(prompt):\n",
|
| 254 |
" input_ids = tokenizer.encode(prompt, return_tensors='pt').to(device)\n",
|