File size: 16,638 Bytes
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 |
{
"cells": [
{
"cell_type": "markdown",
"id": "b1b37ca8-25a3-440c-9b68-7f72ce670ade",
"metadata": {},
"source": [
"# 2.4 基因大模型的生物序列特征提取"
]
},
{
"cell_type": "markdown",
"id": "d3d04215-2b6c-41fb-92a4-90c82d322ba4",
"metadata": {},
"source": [
"使用 GPT-2 模型获取文本的特征向量是一个常见的需求,尤其是在进行文本分类、相似度计算或其他下游任务时。Hugging Face 的 transformers 库提供了简单易用的接口来实现这一点。以下是详细的步骤和代码示例,帮助你从 GPT-2 模型中提取文本的特征向量。"
]
},
{
"cell_type": "markdown",
"id": "3ff5b7c6-e57c-4839-8510-f764154faa65",
"metadata": {},
"source": [
"使用 GPT-2 模型获取文本的特征向量是一个常见的需求,尤其是在进行文本分类、相似度计算或其他下游任务时。Hugging Face 的 `transformers` 库提供了简单易用的接口来实现这一点。以下是详细的步骤和代码示例,帮助你从 GPT-2 模型中提取文本的特征向量。\n",
"\n",
"### 方法 1: 使用隐藏状态(Hidden States)\n",
"\n",
"GPT-2 是一个基于 Transformer 的语言模型,它在每一层都有隐藏状态(hidden states),这些隐藏状态可以作为文本的特征表示。你可以选择最后一层的隐藏状态作为最终的特征向量,或者对多层的隐藏状态进行平均或拼接。\n",
"\n",
"\n",
"### 方法 2: 使用池化策略\n",
"\n",
"另一种方法是通过对所有 token 的隐藏状态进行池化操作来获得句子级别的特征向量。常见的池化方法包括:\n",
"\n",
"- **均值池化**(Mean Pooling):对所有 token 的隐藏状态求平均。\n",
"- **最大池化**(Max Pooling):对每个维度取最大值。"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "e7fe053b-d6da-488a-9c62-24e4b40a992d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'input_ids': tensor([[ 1, 191, 29, 753, 1241, 2104, 12297, 357, 85, 4395,\n",
" 26392, 16]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}\n",
"torch.Size([768])\n",
"torch.Size([768])\n",
"torch.Size([768])\n"
]
}
],
"source": [
"from transformers import AutoTokenizer, AutoModel\n",
"tokenizer = AutoTokenizer.from_pretrained('dna_bpe_dict')\n",
"tokenizer.tokenize(\"GAGCACATTCGCCTGCGTGCGCACTCACACACACGTTCAAAAAGAGTCCATTCGATTCTGGCAGTAG\")\n",
"#result: [G','AGCAC','ATTCGCC',....]\n",
"\n",
"model = AutoModel.from_pretrained('dna_gpt2_v0')\n",
"import torch\n",
"dna = \"ACGTAGCATCGGATCTATCTATCGACACTTGGTTATCGATCTACGAGCATCTCGTTAGC\"\n",
"inputs = tokenizer(dna, return_tensors = 'pt')\n",
"print(inputs)\n",
"\n",
"outputs = model(inputs[\"input_ids\"])\n",
"#outputs = model(**inputs)\n",
"\n",
"hidden_states = outputs.last_hidden_state # [1, sequence_length, 768] outputs.last_hidden_state or outputs[0]\n",
"\n",
"# embedding with mean pooling\n",
"embedding_mean = torch.mean(hidden_states[0], dim=0)\n",
"print(embedding_mean.shape) # expect to be 768\n",
"\n",
"# embedding with max pooling\n",
"embedding_max = torch.max(hidden_states[0], dim=0)[0]\n",
"print(embedding_max.shape) # expect to be 768\n",
"\n",
"# embedding with first token\n",
"embedding_first_token = hidden_states[0][0]\n",
"print(embedding_first_token.shape) # expect to be 768"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "a1f2b545-283a-4613-a953-beb82f427826",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Some weights of BertModel were not initialized from the model checkpoint at dna_bert_v0 and are newly initialized: ['bert.pooler.dense.bias', 'bert.pooler.dense.weight']\n",
"You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'input_ids': tensor([[ 6, 200, 16057, 10, 1256, 2123, 12294, 366, 13138, 7826,\n",
" 82, 25]]), 'token_type_ids': tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}\n",
"torch.Size([768])\n",
"torch.Size([768])\n",
"torch.Size([768])\n"
]
}
],
"source": [
"from transformers import AutoTokenizer, AutoModel\n",
"import torch\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained('dna_wordpiece_dict')\n",
"tokenizer.tokenize(\"GAGCACATTCGCCTGCGTGCGCACTCACACACACGTTCAAAAAGAGTCCATTCGATTCTGGCAGTAG\")\n",
"#result: [G','AGCAC','ATTCGCC',....]\n",
"\n",
"model = AutoModel.from_pretrained('dna_bert_v0')\n",
"dna = \"ACGTAGCATCGGATCTATCTATCGACACTTGGTTATCGATCTACGAGCATCTCGTTAGC\"\n",
"inputs = tokenizer(dna, return_tensors = 'pt')\n",
"print(inputs)\n",
"\n",
"outputs = model(inputs[\"input_ids\"])\n",
"#outputs = model(**inputs)\n",
"\n",
"hidden_states = outputs.last_hidden_state # [1, sequence_length, 768] outputs.last_hidden_state or outputs[0]\n",
"\n",
"# embedding with mean pooling\n",
"embedding_mean = torch.mean(hidden_states[0], dim=0)\n",
"print(embedding_mean.shape) # expect to be 768\n",
"\n",
"# embedding with max pooling\n",
"embedding_max = torch.max(hidden_states[0], dim=0)[0]\n",
"print(embedding_max.shape) # expect to be 768\n",
"\n",
"# embedding with first token\n",
"embedding_first_token = hidden_states[0][0]\n",
"print(embedding_first_token.shape) # expect to be 768"
]
},
{
"cell_type": "markdown",
"id": "56761874-9af7-4b90-aa8b-131e5b8c69b6",
"metadata": {},
"source": [
"## 特征提取并分类\n",
"\n",
"我们使用第一章中的\"dnagpt/dna_core_promoter\"数据集,演示下使用我们训练的DNA GPT2或者DNA bert模型,提取序列特征,然使用最基础的逻辑回归分类方法,对序列进行分类。"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "f1ca177c-a80f-48a1-b2f9-16c13b3350db",
"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": 45,
"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",
"\"\"\"\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": 46,
"id": "2295739c-e80a-47be-9400-88bfab4b0bb6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatasetDict({\n",
" train: Dataset({\n",
" features: ['sequence', 'label'],\n",
" num_rows: 59196\n",
" })\n",
"})"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from datasets import load_dataset\n",
"dna_data = load_dataset(\"dnagpt/dna_core_promoter\")\n",
"dna_data"
]
},
{
"cell_type": "markdown",
"id": "c804bced-f151-43a7-8a95-156db358da3e",
"metadata": {},
"source": [
"这里,我们不需要关注这个数据的具体生物学含义,只需知道sequence是具体的DNA序列,label是分类标签,有两个类别0和1即可"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "9a47a1b1-21f2-4d71-801c-50f88e326ed3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'sequence': 'CATGCGGGTCGATATCCTATCTGAATCTCTCAGCCCAAGAGGGAGTCCGCTCATCTATTCGGCAGTACTG',\n",
" 'label': 0}"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dna_data[\"train\"][0]"
]
},
{
"cell_type": "markdown",
"id": "cde7986d-a225-41ca-8f11-614d079fd2bf",
"metadata": {},
"source": [
"这里使用scikit-learn库来构建逻辑回归分类器。首先是特征提取:"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "4010d991-056a-43ce-8cca-30eeec8678f5",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.datasets import load_iris\n",
"from sklearn.metrics import accuracy_score\n",
"\n",
"\n",
"def get_gpt2_feature(sequence):\n",
" return \n",
"\n",
"# 加载数据集\n",
"data = load_iris()\n",
"X = data.data[data.target < 2] # 只选择前两个类别\n",
"y = data.target[data.target < 2]\n",
"\n",
"X = []\n",
"Y = []\n",
"\n",
"for item in dna_data[\"train\"]:\n",
" sequence = item[\"sequence\"]\n",
" label = item[\"label\"]\n",
" x_v = get_gpt2_feature(sequence)\n",
" y_v = label\n",
" X.append(x_v)\n",
" Y.append(y_v)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "8af0effa-b2b6-4e49-9256-cead146d848c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[5.1, 3.5, 1.4, 0.2],\n",
" [4.9, 3. , 1.4, 0.2],\n",
" [4.7, 3.2, 1.3, 0.2],\n",
" [4.6, 3.1, 1.5, 0.2],\n",
" [5. , 3.6, 1.4, 0.2],\n",
" [5.4, 3.9, 1.7, 0.4],\n",
" [4.6, 3.4, 1.4, 0.3],\n",
" [5. , 3.4, 1.5, 0.2],\n",
" [4.4, 2.9, 1.4, 0.2],\n",
" [4.9, 3.1, 1.5, 0.1],\n",
" [5.4, 3.7, 1.5, 0.2],\n",
" [4.8, 3.4, 1.6, 0.2],\n",
" [4.8, 3. , 1.4, 0.1],\n",
" [4.3, 3. , 1.1, 0.1],\n",
" [5.8, 4. , 1.2, 0.2],\n",
" [5.7, 4.4, 1.5, 0.4],\n",
" [5.4, 3.9, 1.3, 0.4],\n",
" [5.1, 3.5, 1.4, 0.3],\n",
" [5.7, 3.8, 1.7, 0.3],\n",
" [5.1, 3.8, 1.5, 0.3],\n",
" [5.4, 3.4, 1.7, 0.2],\n",
" [5.1, 3.7, 1.5, 0.4],\n",
" [4.6, 3.6, 1. , 0.2],\n",
" [5.1, 3.3, 1.7, 0.5],\n",
" [4.8, 3.4, 1.9, 0.2],\n",
" [5. , 3. , 1.6, 0.2],\n",
" [5. , 3.4, 1.6, 0.4],\n",
" [5.2, 3.5, 1.5, 0.2],\n",
" [5.2, 3.4, 1.4, 0.2],\n",
" [4.7, 3.2, 1.6, 0.2],\n",
" [4.8, 3.1, 1.6, 0.2],\n",
" [5.4, 3.4, 1.5, 0.4],\n",
" [5.2, 4.1, 1.5, 0.1],\n",
" [5.5, 4.2, 1.4, 0.2],\n",
" [4.9, 3.1, 1.5, 0.2],\n",
" [5. , 3.2, 1.2, 0.2],\n",
" [5.5, 3.5, 1.3, 0.2],\n",
" [4.9, 3.6, 1.4, 0.1],\n",
" [4.4, 3. , 1.3, 0.2],\n",
" [5.1, 3.4, 1.5, 0.2],\n",
" [5. , 3.5, 1.3, 0.3],\n",
" [4.5, 2.3, 1.3, 0.3],\n",
" [4.4, 3.2, 1.3, 0.2],\n",
" [5. , 3.5, 1.6, 0.6],\n",
" [5.1, 3.8, 1.9, 0.4],\n",
" [4.8, 3. , 1.4, 0.3],\n",
" [5.1, 3.8, 1.6, 0.2],\n",
" [4.6, 3.2, 1.4, 0.2],\n",
" [5.3, 3.7, 1.5, 0.2],\n",
" [5. , 3.3, 1.4, 0.2],\n",
" [7. , 3.2, 4.7, 1.4],\n",
" [6.4, 3.2, 4.5, 1.5],\n",
" [6.9, 3.1, 4.9, 1.5],\n",
" [5.5, 2.3, 4. , 1.3],\n",
" [6.5, 2.8, 4.6, 1.5],\n",
" [5.7, 2.8, 4.5, 1.3],\n",
" [6.3, 3.3, 4.7, 1.6],\n",
" [4.9, 2.4, 3.3, 1. ],\n",
" [6.6, 2.9, 4.6, 1.3],\n",
" [5.2, 2.7, 3.9, 1.4],\n",
" [5. , 2. , 3.5, 1. ],\n",
" [5.9, 3. , 4.2, 1.5],\n",
" [6. , 2.2, 4. , 1. ],\n",
" [6.1, 2.9, 4.7, 1.4],\n",
" [5.6, 2.9, 3.6, 1.3],\n",
" [6.7, 3.1, 4.4, 1.4],\n",
" [5.6, 3. , 4.5, 1.5],\n",
" [5.8, 2.7, 4.1, 1. ],\n",
" [6.2, 2.2, 4.5, 1.5],\n",
" [5.6, 2.5, 3.9, 1.1],\n",
" [5.9, 3.2, 4.8, 1.8],\n",
" [6.1, 2.8, 4. , 1.3],\n",
" [6.3, 2.5, 4.9, 1.5],\n",
" [6.1, 2.8, 4.7, 1.2],\n",
" [6.4, 2.9, 4.3, 1.3],\n",
" [6.6, 3. , 4.4, 1.4],\n",
" [6.8, 2.8, 4.8, 1.4],\n",
" [6.7, 3. , 5. , 1.7],\n",
" [6. , 2.9, 4.5, 1.5],\n",
" [5.7, 2.6, 3.5, 1. ],\n",
" [5.5, 2.4, 3.8, 1.1],\n",
" [5.5, 2.4, 3.7, 1. ],\n",
" [5.8, 2.7, 3.9, 1.2],\n",
" [6. , 2.7, 5.1, 1.6],\n",
" [5.4, 3. , 4.5, 1.5],\n",
" [6. , 3.4, 4.5, 1.6],\n",
" [6.7, 3.1, 4.7, 1.5],\n",
" [6.3, 2.3, 4.4, 1.3],\n",
" [5.6, 3. , 4.1, 1.3],\n",
" [5.5, 2.5, 4. , 1.3],\n",
" [5.5, 2.6, 4.4, 1.2],\n",
" [6.1, 3. , 4.6, 1.4],\n",
" [5.8, 2.6, 4. , 1.2],\n",
" [5. , 2.3, 3.3, 1. ],\n",
" [5.6, 2.7, 4.2, 1.3],\n",
" [5.7, 3. , 4.2, 1.2],\n",
" [5.7, 2.9, 4.2, 1.3],\n",
" [6.2, 2.9, 4.3, 1.3],\n",
" [5.1, 2.5, 3. , 1.1],\n",
" [5.7, 2.8, 4.1, 1.3]])"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "868a3cab-e991-4990-9ec5-3e632a41a599",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ab0c188-6476-43c4-b361-a2bfe0ec7a8a",
"metadata": {},
"outputs": [],
"source": [
"# 将数据分为训练集和测试集\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
"\n",
"# 创建逻辑回归模型\n",
"model = LogisticRegression()\n",
"\n",
"# 训练模型\n",
"model.fit(X_train, y_train)\n",
"\n",
"# 在测试集上进行预测\n",
"y_pred = model.predict(X_test)\n",
"\n",
"# 计算准确率\n",
"accuracy = accuracy_score(y_test, y_pred)\n",
"print(f\"Accuracy: {accuracy * 100:.2f}%\")\n",
"\n",
"# 输出部分预测结果与真实标签对比\n",
"for i in range(5):\n",
" print(f\"True: {y_test[i]}, Predicted: {y_pred[i]}\")"
]
}
],
"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
}
|