File size: 17,719 Bytes
ca90249 1df7ad4 ca90249 1df7ad4 ca90249 | 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | {
"cells": [
{
"cell_type": "markdown",
"id": "c499a5c3-0244-41c4-9947-e166206204e2",
"metadata": {},
"source": [
"# 3.5 回归类任务"
]
},
{
"cell_type": "markdown",
"id": "4678171b-bbc8-49dd-ad04-48f5ef89b45e",
"metadata": {},
"source": [
"GPT-2 原本是设计用于生成自然语言的模型,但通过适当的调整和微调,它也可以用于回归任务,例如预测连续值。\n",
"\n",
"使用 GPT-2 进行回归问题的解决,可以将回归问题转化为自回归语言模型任务。GPT-2 原本是设计用于生成自然语言的模型,但通过适当的调整和微调,它也可以用于回归任务,例如预测连续值(如情感评分、价格预测等)。\n",
"\n",
"---\n",
"\n",
"### **1. 使用 GPT-2 做回归的核心思路**\n",
"\n",
"1. **调整输出层**:\n",
" - 默认情况下,GPT-2 的输出是一个词汇表大小的概率分布,用于预测下一个 token。\n",
" - 对于回归问题,可以将模型的最后一层替换为一个线性层,使得输出变为一个标量或多个连续值。\n",
" - gpt2的huggingface实现中,可以简单设置1个分类的分类header,实现回归预测。\n",
"\n",
"2. **损失函数**:\n",
" - 对于回归问题,使用均方误差(MSE)或均绝对误差(MAE)作为损失函数,而不是分类任务中常用的交叉熵。\n",
"\n",
"3. **输入格式**:\n",
" - 输入数据仍然是文本,可以通过特定的模板形式加入上下文信息。\n",
"\n",
"---\n",
"\n",
"### **2. GPT-2 回归任务的实现步骤**\n",
"\n",
"#### **(1)加载基础模型**\n",
"\n",
"从 Hugging Face Transformers 库加载 GPT-2 模型和分词器,并调整其配置以适应回归任务。\n",
"\n",
"```python\n",
"from transformers import GPT2Tokenizer, GPT2Model, GPT2Config, AutoModelForSequenceClassification\n",
"\n",
"# 加载分词器\n",
"tokenizer = GPT2Tokenizer.from_pretrained(\"gpt2\")\n",
"\n",
"# 调整模型配置,num_labels=1 表示回归任务\n",
"config = GPT2Config.from_pretrained(\"gpt2\", num_labels=1)\n",
"\n",
"# 加载模型,增加回归输出\n",
"model = AutoModelForSequenceClassification.from_pretrained(\"gpt2\", config=config)\n",
"```\n",
"\n",
"---\n",
"\n",
"### **3. 课程数据集**\n",
"\n",
"本例程使用了蛋白质稳定性分析的数据集,也就是一个蛋白质序列,对应一个float的数值,做回归预测分析。\n",
"\n",
"**蛋白质稳定性分析**是研究蛋白质在不同条件下保持其结构和功能的能力的过程。蛋白质稳定性是生物化学和生物技术领域的重要课题,影响着蛋白质的折叠、功能执行、以及在应用中的可用性(如工业酶、药物开发等)。\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "1e8c0f86-af78-43e1-8db4-e2a2ea22f815",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"\\nimport os\\n\\n# 设置环境变量, autodl专区 其他idc\\nos.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'\\n\\n# 打印环境变量以确认设置成功\\nprint(os.environ.get('HF_ENDPOINT'))\\n\""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import subprocess\n",
"import os\n",
"# 设置环境变量, autodl一般区域\n",
"result = subprocess.run('bash -c \"source /etc/network_turbo && env | grep proxy\"', shell=True, capture_output=True, text=True)\n",
"output = result.stdout\n",
"for line in output.splitlines():\n",
" if '=' in line:\n",
" var, value = line.split('=', 1)\n",
" os.environ[var] = value\n",
"\n",
"\"\"\"\n",
"import os\n",
"\n",
"# 设置环境变量, autodl专区 其他idc\n",
"os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'\n",
"\n",
"# 打印环境变量以确认设置成功\n",
"print(os.environ.get('HF_ENDPOINT'))\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c51a8d69-9a36-47e7-8084-f64e6a72e4f7",
"metadata": {},
"outputs": [],
"source": [
"from transformers import AutoTokenizer, AutoModel\n",
"from tokenizers import Tokenizer\n",
"from transformers import GPT2LMHeadModel, AutoConfig,GPT2Tokenizer\n",
"from transformers import AutoModelForSequenceClassification\n",
"from transformers import DataCollatorWithPadding"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a5aeb7c1-2d2a-4f57-ad8c-659613870e59",
"metadata": {},
"outputs": [],
"source": [
"#set tokenizer\n",
"tokenizer = GPT2Tokenizer.from_pretrained(\"dnagpt/gene_eng_gpt2_v0\")\n",
"tokenizer.pad_token = tokenizer.eos_token"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ad0c19cd-96a5-463e-8b7d-439646fef429",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Some weights of GPT2ForSequenceClassification were not initialized from the model checkpoint at dnagpt/gene_eng_gpt2_v0 and are newly initialized: ['score.weight']\n",
"You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n"
]
}
],
"source": [
"#set model\n",
"model = AutoModelForSequenceClassification.from_pretrained('dnagpt/gene_eng_gpt2_v0', num_labels=1)\n",
"model.config.pad_token_id = model.config.eos_token_id"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "8c48cb0a-6142-4afc-823e-08fb33f74222",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatasetDict({\n",
" train: Dataset({\n",
" features: ['seq_id', 'seq_type', 'seq', 'label'],\n",
" num_rows: 62079\n",
" })\n",
" test: Dataset({\n",
" features: ['seq_id', 'seq_type', 'seq', 'label'],\n",
" num_rows: 6898\n",
" })\n",
"})"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from datasets import load_dataset\n",
"# 1. load ~11k samples from promoters prediction dataset\n",
"dataset = load_dataset(\"csv\", data_files=\"data/protein_stab.csv\")['train'].train_test_split(test_size=0.1)\n",
"dataset"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "685dd025-f00a-4869-bc30-9843c77b6d8a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'seq_id': 'train_prot_32672',\n",
" 'seq_type': 'prot',\n",
" 'seq': 'FYRLIIFKYPDYIDTYLRLAAIAKEKNNLQLSIEGNGSGGNGSGGNGSGN',\n",
" 'label': 0.7599999904632561}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataset[\"train\"][0]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6e10dbbb-73ef-4b67-8290-77f8896298f5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"datasets mean token lenght 17.24006958538707 min token length 12 max token length 35\n"
]
}
],
"source": [
"token_len_list = []\n",
"for item in dataset[\"test\"]:\n",
" inputs = tokenizer.tokenize(item[\"seq\"])\n",
" token_len_list.append( len(inputs) )\n",
"\n",
"mean_len = sum(token_len_list)/len(token_len_list)\n",
"min_len = min(token_len_list)\n",
"max_len = max(token_len_list)\n",
"\n",
"print(\"datasets \", \"mean token lenght\", mean_len, \"min token length\", min_len, \"max token length\", max_len)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "ac58b5b4-bff0-404d-bcf5-2b93db2b37c0",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "419cce8c5ba249ac8c8773dd2d69992d",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map: 0%| | 0/62079 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Asking to pad to max_length but no maximum length is provided and the model has no predefined maximum length. Default to no padding.\n",
"Asking to truncate to max_length but no maximum length is provided and the model has no predefined maximum length. Default to no truncation.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0b9ea09fe3ea49b19f7d52aca7949acf",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map: 0%| | 0/6898 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# 2. tokenize\n",
"def tokenize_function(examples):\n",
" return tokenizer(examples['seq'], truncation=True, padding='max_length')\n",
"\n",
"# 3. 对数据集应用分词函数\n",
"tokenized_datasets = dataset.map(tokenize_function, batched=True)\n",
"\n",
"# 4. 创建一个数据收集器,用于动态填充和遮蔽\n",
"data_collator = DataCollatorWithPadding(tokenizer=tokenizer)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "94f6d643-2cf7-4651-9a8d-1884b2bddd1c",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/root/miniconda3/lib/python3.12/site-packages/transformers/training_args.py:1575: FutureWarning: `evaluation_strategy` is deprecated and will be removed in version 4.46 of 🤗 Transformers. Use `eval_strategy` instead\n",
" warnings.warn(\n",
"/tmp/ipykernel_1347/4285456223.py:23: FutureWarning: `tokenizer` is deprecated and will be removed in version 5.0.0 for `Trainer.__init__`. Use `processing_class` instead.\n",
" trainer = Trainer(\n"
]
}
],
"source": [
"from transformers import TrainingArguments, Trainer\n",
"import numpy as np\n",
"from sklearn.metrics import mean_squared_error\n",
"\n",
"\n",
"def compute_metrics(eval_pred):\n",
" predictions, labels = eval_pred\n",
" rmse = mean_squared_error(labels, predictions)\n",
" return {\"rmse\": rmse}\n",
"\n",
"# 设置训练参数\n",
"training_args = TrainingArguments(\n",
" output_dir='./results',\n",
" evaluation_strategy=\"epoch\",\n",
" learning_rate=2e-5,\n",
" per_device_train_batch_size=20,\n",
" per_device_eval_batch_size=20,\n",
" num_train_epochs=10,\n",
" weight_decay=0.01,\n",
")\n",
"\n",
"# 使用Trainer API进行训练(假设已有train_dataset和eval_dataset)\n",
"trainer = Trainer(\n",
" model=model,\n",
" args=training_args,\n",
" train_dataset=tokenized_datasets[\"train\"],\n",
" eval_dataset=tokenized_datasets[\"test\"],\n",
" tokenizer=tokenizer,\n",
" data_collator=data_collator,\n",
" compute_metrics=compute_metrics,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dfe12979-d977-4404-bf9e-18c1f91a3e39",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='30987' max='31040' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [30987/31040 1:00:56 < 00:06, 8.47 it/s, Epoch 9.98/10]\n",
" </div>\n",
" <table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: left;\">\n",
" <th>Epoch</th>\n",
" <th>Training Loss</th>\n",
" <th>Validation Loss</th>\n",
" <th>Rmse</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>0.044600</td>\n",
" <td>0.163462</td>\n",
" <td>0.163462</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>0.041900</td>\n",
" <td>0.157900</td>\n",
" <td>0.157900</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>0.037700</td>\n",
" <td>0.159724</td>\n",
" <td>0.159724</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4</td>\n",
" <td>0.031700</td>\n",
" <td>0.157686</td>\n",
" <td>0.157686</td>\n",
" </tr>\n",
" <tr>\n",
" <td>5</td>\n",
" <td>0.028800</td>\n",
" <td>0.157124</td>\n",
" <td>0.157124</td>\n",
" </tr>\n",
" <tr>\n",
" <td>6</td>\n",
" <td>0.025400</td>\n",
" <td>0.150852</td>\n",
" <td>0.150852</td>\n",
" </tr>\n",
" <tr>\n",
" <td>7</td>\n",
" <td>0.022300</td>\n",
" <td>0.159293</td>\n",
" <td>0.159293</td>\n",
" </tr>\n",
" <tr>\n",
" <td>8</td>\n",
" <td>0.019600</td>\n",
" <td>0.154608</td>\n",
" <td>0.154608</td>\n",
" </tr>\n",
" <tr>\n",
" <td>9</td>\n",
" <td>0.017300</td>\n",
" <td>0.156104</td>\n",
" <td>0.156104</td>\n",
" </tr>\n",
" </tbody>\n",
"</table><p>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"IOPub message rate exceeded.\n",
"The Jupyter server will temporarily stop sending output\n",
"to the client in order to avoid crashing it.\n",
"To change this limit, set the config variable\n",
"`--ServerApp.iopub_msg_rate_limit`.\n",
"\n",
"Current values:\n",
"ServerApp.iopub_msg_rate_limit=1000.0 (msgs/sec)\n",
"ServerApp.rate_limit_window=3.0 (secs)\n",
"\n"
]
}
],
"source": [
"# 开始训练\n",
"trainer.train()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "060c4618-40d0-4934-bab8-36aab3a46de5",
"metadata": {},
"outputs": [],
"source": [
"#模型测试\n",
"predictions = trainer.predict(tokenized_datasets[\"test\"])\n",
"predictions"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "1f8ef885-5bc9-4668-905b-6b2235209654",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='345' max='345' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [345/345 00:09]\n",
" </div>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"{'eval_loss': 0.15949687361717224,\n",
" 'eval_rmse': 0.15949687361717224,\n",
" 'eval_runtime': 9.1483,\n",
" 'eval_samples_per_second': 754.017,\n",
" 'eval_steps_per_second': 37.712,\n",
" 'epoch': 10.0}"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"trainer.evaluate()"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "afabdbe9-9b96-4f9e-bef2-1d819431f8d1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 1.7208484 ]\n",
" [ 0.00225139]\n",
" [ 0.3325616 ]\n",
" [-0.34372616]\n",
" [-0.45505935]\n",
" [-0.06892765]\n",
" [ 0.15099108]\n",
" [ 0.12211376]\n",
" [ 0.3947332 ]\n",
" [ 0.23186803]]\n"
]
}
],
"source": [
"predictions.predictions[0:10].squeeze()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "fa9d17fd-eece-4c1e-99e0-3d19d36f7584",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1.69, 0.84, 0.58, -0.15, 0.23, 0.03, 0.15, 0.2 , 0.51,\n",
" 1.1 ], dtype=float32)"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"predictions.label_ids[0:10]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52252015-e068-414b-bd8a-79a5d1a2beec",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|