Upload 8 files
Browse files- NER_Customer_final.ipynb +624 -0
- README.md +97 -0
- config.json +25 -0
- model_full_v3.pth +3 -0
- pytorch_model.bin +3 -0
- pytorch_model_v3.bin +3 -0
- requirements.txt +12 -0
- vocab.txt +0 -0
NER_Customer_final.ipynb
ADDED
|
@@ -0,0 +1,624 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
| 12 |
+
"Using TensorFlow backend.\n"
|
| 13 |
+
]
|
| 14 |
+
}
|
| 15 |
+
],
|
| 16 |
+
"source": [
|
| 17 |
+
"import pandas as pd\n",
|
| 18 |
+
"import math\n",
|
| 19 |
+
"import numpy as np\n",
|
| 20 |
+
"from seqeval.metrics import f1_score\n",
|
| 21 |
+
"from seqeval.metrics import classification_report,accuracy_score,f1_score\n",
|
| 22 |
+
"import torch.nn.functional as F\n",
|
| 23 |
+
"import torch\n",
|
| 24 |
+
"import os\n",
|
| 25 |
+
"from tqdm import tqdm,trange\n",
|
| 26 |
+
"from torch.optim import Adam\n",
|
| 27 |
+
"from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler\n",
|
| 28 |
+
"from keras.preprocessing.sequence import pad_sequences\n",
|
| 29 |
+
"from pytorch_transformers import BertTokenizer, BertConfig\n",
|
| 30 |
+
"import torch\n",
|
| 31 |
+
"import torch.nn.functional as F\n",
|
| 32 |
+
"from pytorch_transformers import BertForTokenClassification, AdamW"
|
| 33 |
+
]
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"cell_type": "code",
|
| 37 |
+
"execution_count": 2,
|
| 38 |
+
"metadata": {},
|
| 39 |
+
"outputs": [],
|
| 40 |
+
"source": [
|
| 41 |
+
"tag2idx = {'B-Address': 0,\n",
|
| 42 |
+
" 'B-Details': 1,\n",
|
| 43 |
+
" 'B-Auth_Status': 2,\n",
|
| 44 |
+
" 'B-Product_Name': 3,\n",
|
| 45 |
+
" 'B-Verification_Method': 4,\n",
|
| 46 |
+
" 'B-Category': 5,\n",
|
| 47 |
+
" 'B-Person': 6,\n",
|
| 48 |
+
" 'B-Complaint_Category': 7,\n",
|
| 49 |
+
" 'B-Field': 8,\n",
|
| 50 |
+
" 'B-Description': 9,\n",
|
| 51 |
+
" 'B-Divert': 10,\n",
|
| 52 |
+
" 'B-Auth_Method': 11,\n",
|
| 53 |
+
" 'B-Address_Status': 12,\n",
|
| 54 |
+
" 'B-Team': 13,\n",
|
| 55 |
+
" 'B-Service_Name': 14,\n",
|
| 56 |
+
" 'B-Status': 15,\n",
|
| 57 |
+
" 'I-Address': 16,\n",
|
| 58 |
+
" 'I-Details': 17,\n",
|
| 59 |
+
" 'I-Auth_Status': 18,\n",
|
| 60 |
+
" 'I-Product_Name': 19,\n",
|
| 61 |
+
" 'I-Verification_Method': 20,\n",
|
| 62 |
+
" 'I-Category': 21,\n",
|
| 63 |
+
" 'I-Person': 22,\n",
|
| 64 |
+
" 'I-Complaint_Category': 23,\n",
|
| 65 |
+
" 'I-Field': 24,\n",
|
| 66 |
+
" 'I-Description': 25,\n",
|
| 67 |
+
" 'I-Divert': 26,\n",
|
| 68 |
+
" 'I-Auth_Method': 27,\n",
|
| 69 |
+
" 'I-Address_Status': 28,\n",
|
| 70 |
+
" 'I-Team': 29,\n",
|
| 71 |
+
" 'I-Service_Name': 30,\n",
|
| 72 |
+
" 'I-Status': 31,\n",
|
| 73 |
+
" 'O': 32,\n",
|
| 74 |
+
" 'X': 33,\n",
|
| 75 |
+
" '[CLS]': 34,\n",
|
| 76 |
+
" '[SEP]': 35}\n"
|
| 77 |
+
]
|
| 78 |
+
},
|
| 79 |
+
{
|
| 80 |
+
"cell_type": "code",
|
| 81 |
+
"execution_count": 3,
|
| 82 |
+
"metadata": {},
|
| 83 |
+
"outputs": [],
|
| 84 |
+
"source": [
|
| 85 |
+
"tag2name={tag2idx[key] : key for key in tag2idx.keys()}"
|
| 86 |
+
]
|
| 87 |
+
},
|
| 88 |
+
{
|
| 89 |
+
"cell_type": "code",
|
| 90 |
+
"execution_count": 4,
|
| 91 |
+
"metadata": {},
|
| 92 |
+
"outputs": [
|
| 93 |
+
{
|
| 94 |
+
"data": {
|
| 95 |
+
"text/plain": [
|
| 96 |
+
"{0: 'B-Address',\n",
|
| 97 |
+
" 1: 'B-Details',\n",
|
| 98 |
+
" 2: 'B-Auth_Status',\n",
|
| 99 |
+
" 3: 'B-Product_Name',\n",
|
| 100 |
+
" 4: 'B-Verification_Method',\n",
|
| 101 |
+
" 5: 'B-Category',\n",
|
| 102 |
+
" 6: 'B-Person',\n",
|
| 103 |
+
" 7: 'B-Complaint_Category',\n",
|
| 104 |
+
" 8: 'B-Field',\n",
|
| 105 |
+
" 9: 'B-Description',\n",
|
| 106 |
+
" 10: 'B-Divert',\n",
|
| 107 |
+
" 11: 'B-Auth_Method',\n",
|
| 108 |
+
" 12: 'B-Address_Status',\n",
|
| 109 |
+
" 13: 'B-Team',\n",
|
| 110 |
+
" 14: 'B-Service_Name',\n",
|
| 111 |
+
" 15: 'B-Status',\n",
|
| 112 |
+
" 16: 'I-Address',\n",
|
| 113 |
+
" 17: 'I-Details',\n",
|
| 114 |
+
" 18: 'I-Auth_Status',\n",
|
| 115 |
+
" 19: 'I-Product_Name',\n",
|
| 116 |
+
" 20: 'I-Verification_Method',\n",
|
| 117 |
+
" 21: 'I-Category',\n",
|
| 118 |
+
" 22: 'I-Person',\n",
|
| 119 |
+
" 23: 'I-Complaint_Category',\n",
|
| 120 |
+
" 24: 'I-Field',\n",
|
| 121 |
+
" 25: 'I-Description',\n",
|
| 122 |
+
" 26: 'I-Divert',\n",
|
| 123 |
+
" 27: 'I-Auth_Method',\n",
|
| 124 |
+
" 28: 'I-Address_Status',\n",
|
| 125 |
+
" 29: 'I-Team',\n",
|
| 126 |
+
" 30: 'I-Service_Name',\n",
|
| 127 |
+
" 31: 'I-Status',\n",
|
| 128 |
+
" 32: 'O',\n",
|
| 129 |
+
" 33: 'X',\n",
|
| 130 |
+
" 34: '[CLS]',\n",
|
| 131 |
+
" 35: '[SEP]'}"
|
| 132 |
+
]
|
| 133 |
+
},
|
| 134 |
+
"execution_count": 4,
|
| 135 |
+
"metadata": {},
|
| 136 |
+
"output_type": "execute_result"
|
| 137 |
+
}
|
| 138 |
+
],
|
| 139 |
+
"source": [
|
| 140 |
+
"tag2name"
|
| 141 |
+
]
|
| 142 |
+
},
|
| 143 |
+
{
|
| 144 |
+
"cell_type": "code",
|
| 145 |
+
"execution_count": 9,
|
| 146 |
+
"metadata": {},
|
| 147 |
+
"outputs": [
|
| 148 |
+
{
|
| 149 |
+
"data": {
|
| 150 |
+
"text/plain": [
|
| 151 |
+
"BertForTokenClassification(\n",
|
| 152 |
+
" (bert): BertModel(\n",
|
| 153 |
+
" (embeddings): BertEmbeddings(\n",
|
| 154 |
+
" (word_embeddings): Embedding(30522, 768, padding_idx=0)\n",
|
| 155 |
+
" (position_embeddings): Embedding(512, 768)\n",
|
| 156 |
+
" (token_type_embeddings): Embedding(2, 768)\n",
|
| 157 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 158 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 159 |
+
" )\n",
|
| 160 |
+
" (encoder): BertEncoder(\n",
|
| 161 |
+
" (layer): ModuleList(\n",
|
| 162 |
+
" (0): BertLayer(\n",
|
| 163 |
+
" (attention): BertAttention(\n",
|
| 164 |
+
" (self): BertSelfAttention(\n",
|
| 165 |
+
" (query): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 166 |
+
" (key): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 167 |
+
" (value): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 168 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 169 |
+
" )\n",
|
| 170 |
+
" (output): BertSelfOutput(\n",
|
| 171 |
+
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 172 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 173 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 174 |
+
" )\n",
|
| 175 |
+
" )\n",
|
| 176 |
+
" (intermediate): BertIntermediate(\n",
|
| 177 |
+
" (dense): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 178 |
+
" )\n",
|
| 179 |
+
" (output): BertOutput(\n",
|
| 180 |
+
" (dense): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 181 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 182 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 183 |
+
" )\n",
|
| 184 |
+
" )\n",
|
| 185 |
+
" (1): BertLayer(\n",
|
| 186 |
+
" (attention): BertAttention(\n",
|
| 187 |
+
" (self): BertSelfAttention(\n",
|
| 188 |
+
" (query): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 189 |
+
" (key): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 190 |
+
" (value): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 191 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 192 |
+
" )\n",
|
| 193 |
+
" (output): BertSelfOutput(\n",
|
| 194 |
+
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 195 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 196 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 197 |
+
" )\n",
|
| 198 |
+
" )\n",
|
| 199 |
+
" (intermediate): BertIntermediate(\n",
|
| 200 |
+
" (dense): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 201 |
+
" )\n",
|
| 202 |
+
" (output): BertOutput(\n",
|
| 203 |
+
" (dense): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 204 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 205 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 206 |
+
" )\n",
|
| 207 |
+
" )\n",
|
| 208 |
+
" (2): BertLayer(\n",
|
| 209 |
+
" (attention): BertAttention(\n",
|
| 210 |
+
" (self): BertSelfAttention(\n",
|
| 211 |
+
" (query): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 212 |
+
" (key): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 213 |
+
" (value): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 214 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 215 |
+
" )\n",
|
| 216 |
+
" (output): BertSelfOutput(\n",
|
| 217 |
+
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 218 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 219 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 220 |
+
" )\n",
|
| 221 |
+
" )\n",
|
| 222 |
+
" (intermediate): BertIntermediate(\n",
|
| 223 |
+
" (dense): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 224 |
+
" )\n",
|
| 225 |
+
" (output): BertOutput(\n",
|
| 226 |
+
" (dense): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 227 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 228 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 229 |
+
" )\n",
|
| 230 |
+
" )\n",
|
| 231 |
+
" (3): BertLayer(\n",
|
| 232 |
+
" (attention): BertAttention(\n",
|
| 233 |
+
" (self): BertSelfAttention(\n",
|
| 234 |
+
" (query): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 235 |
+
" (key): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 236 |
+
" (value): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 237 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 238 |
+
" )\n",
|
| 239 |
+
" (output): BertSelfOutput(\n",
|
| 240 |
+
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 241 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 242 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 243 |
+
" )\n",
|
| 244 |
+
" )\n",
|
| 245 |
+
" (intermediate): BertIntermediate(\n",
|
| 246 |
+
" (dense): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 247 |
+
" )\n",
|
| 248 |
+
" (output): BertOutput(\n",
|
| 249 |
+
" (dense): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 250 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 251 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 252 |
+
" )\n",
|
| 253 |
+
" )\n",
|
| 254 |
+
" (4): BertLayer(\n",
|
| 255 |
+
" (attention): BertAttention(\n",
|
| 256 |
+
" (self): BertSelfAttention(\n",
|
| 257 |
+
" (query): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 258 |
+
" (key): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 259 |
+
" (value): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 260 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 261 |
+
" )\n",
|
| 262 |
+
" (output): BertSelfOutput(\n",
|
| 263 |
+
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 264 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 265 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 266 |
+
" )\n",
|
| 267 |
+
" )\n",
|
| 268 |
+
" (intermediate): BertIntermediate(\n",
|
| 269 |
+
" (dense): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 270 |
+
" )\n",
|
| 271 |
+
" (output): BertOutput(\n",
|
| 272 |
+
" (dense): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 273 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 274 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 275 |
+
" )\n",
|
| 276 |
+
" )\n",
|
| 277 |
+
" (5): BertLayer(\n",
|
| 278 |
+
" (attention): BertAttention(\n",
|
| 279 |
+
" (self): BertSelfAttention(\n",
|
| 280 |
+
" (query): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 281 |
+
" (key): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 282 |
+
" (value): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 283 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 284 |
+
" )\n",
|
| 285 |
+
" (output): BertSelfOutput(\n",
|
| 286 |
+
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 287 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 288 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 289 |
+
" )\n",
|
| 290 |
+
" )\n",
|
| 291 |
+
" (intermediate): BertIntermediate(\n",
|
| 292 |
+
" (dense): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 293 |
+
" )\n",
|
| 294 |
+
" (output): BertOutput(\n",
|
| 295 |
+
" (dense): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 296 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 297 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 298 |
+
" )\n",
|
| 299 |
+
" )\n",
|
| 300 |
+
" (6): BertLayer(\n",
|
| 301 |
+
" (attention): BertAttention(\n",
|
| 302 |
+
" (self): BertSelfAttention(\n",
|
| 303 |
+
" (query): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 304 |
+
" (key): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 305 |
+
" (value): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 306 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 307 |
+
" )\n",
|
| 308 |
+
" (output): BertSelfOutput(\n",
|
| 309 |
+
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 310 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 311 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 312 |
+
" )\n",
|
| 313 |
+
" )\n",
|
| 314 |
+
" (intermediate): BertIntermediate(\n",
|
| 315 |
+
" (dense): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 316 |
+
" )\n",
|
| 317 |
+
" (output): BertOutput(\n",
|
| 318 |
+
" (dense): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 319 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 320 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 321 |
+
" )\n",
|
| 322 |
+
" )\n",
|
| 323 |
+
" (7): BertLayer(\n",
|
| 324 |
+
" (attention): BertAttention(\n",
|
| 325 |
+
" (self): BertSelfAttention(\n",
|
| 326 |
+
" (query): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 327 |
+
" (key): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 328 |
+
" (value): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 329 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 330 |
+
" )\n",
|
| 331 |
+
" (output): BertSelfOutput(\n",
|
| 332 |
+
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 333 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 334 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 335 |
+
" )\n",
|
| 336 |
+
" )\n",
|
| 337 |
+
" (intermediate): BertIntermediate(\n",
|
| 338 |
+
" (dense): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 339 |
+
" )\n",
|
| 340 |
+
" (output): BertOutput(\n",
|
| 341 |
+
" (dense): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 342 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 343 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 344 |
+
" )\n",
|
| 345 |
+
" )\n",
|
| 346 |
+
" (8): BertLayer(\n",
|
| 347 |
+
" (attention): BertAttention(\n",
|
| 348 |
+
" (self): BertSelfAttention(\n",
|
| 349 |
+
" (query): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 350 |
+
" (key): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 351 |
+
" (value): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 352 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 353 |
+
" )\n",
|
| 354 |
+
" (output): BertSelfOutput(\n",
|
| 355 |
+
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 356 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 357 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 358 |
+
" )\n",
|
| 359 |
+
" )\n",
|
| 360 |
+
" (intermediate): BertIntermediate(\n",
|
| 361 |
+
" (dense): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 362 |
+
" )\n",
|
| 363 |
+
" (output): BertOutput(\n",
|
| 364 |
+
" (dense): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 365 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 366 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 367 |
+
" )\n",
|
| 368 |
+
" )\n",
|
| 369 |
+
" (9): BertLayer(\n",
|
| 370 |
+
" (attention): BertAttention(\n",
|
| 371 |
+
" (self): BertSelfAttention(\n",
|
| 372 |
+
" (query): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 373 |
+
" (key): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 374 |
+
" (value): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 375 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 376 |
+
" )\n",
|
| 377 |
+
" (output): BertSelfOutput(\n",
|
| 378 |
+
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 379 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 380 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 381 |
+
" )\n",
|
| 382 |
+
" )\n",
|
| 383 |
+
" (intermediate): BertIntermediate(\n",
|
| 384 |
+
" (dense): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 385 |
+
" )\n",
|
| 386 |
+
" (output): BertOutput(\n",
|
| 387 |
+
" (dense): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 388 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 389 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 390 |
+
" )\n",
|
| 391 |
+
" )\n",
|
| 392 |
+
" (10): BertLayer(\n",
|
| 393 |
+
" (attention): BertAttention(\n",
|
| 394 |
+
" (self): BertSelfAttention(\n",
|
| 395 |
+
" (query): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 396 |
+
" (key): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 397 |
+
" (value): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 398 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 399 |
+
" )\n",
|
| 400 |
+
" (output): BertSelfOutput(\n",
|
| 401 |
+
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 402 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 403 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 404 |
+
" )\n",
|
| 405 |
+
" )\n",
|
| 406 |
+
" (intermediate): BertIntermediate(\n",
|
| 407 |
+
" (dense): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 408 |
+
" )\n",
|
| 409 |
+
" (output): BertOutput(\n",
|
| 410 |
+
" (dense): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 411 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 412 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 413 |
+
" )\n",
|
| 414 |
+
" )\n",
|
| 415 |
+
" (11): BertLayer(\n",
|
| 416 |
+
" (attention): BertAttention(\n",
|
| 417 |
+
" (self): BertSelfAttention(\n",
|
| 418 |
+
" (query): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 419 |
+
" (key): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 420 |
+
" (value): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 421 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 422 |
+
" )\n",
|
| 423 |
+
" (output): BertSelfOutput(\n",
|
| 424 |
+
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 425 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 426 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 427 |
+
" )\n",
|
| 428 |
+
" )\n",
|
| 429 |
+
" (intermediate): BertIntermediate(\n",
|
| 430 |
+
" (dense): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 431 |
+
" )\n",
|
| 432 |
+
" (output): BertOutput(\n",
|
| 433 |
+
" (dense): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 434 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 435 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 436 |
+
" )\n",
|
| 437 |
+
" )\n",
|
| 438 |
+
" )\n",
|
| 439 |
+
" )\n",
|
| 440 |
+
" (pooler): BertPooler(\n",
|
| 441 |
+
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 442 |
+
" (activation): Tanh()\n",
|
| 443 |
+
" )\n",
|
| 444 |
+
" )\n",
|
| 445 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 446 |
+
" (classifier): Linear(in_features=768, out_features=36, bias=True)\n",
|
| 447 |
+
")"
|
| 448 |
+
]
|
| 449 |
+
},
|
| 450 |
+
"execution_count": 9,
|
| 451 |
+
"metadata": {},
|
| 452 |
+
"output_type": "execute_result"
|
| 453 |
+
}
|
| 454 |
+
],
|
| 455 |
+
"source": [
|
| 456 |
+
"config_path = \"Specify the path\"\n",
|
| 457 |
+
"model_weights_path = \"Specify the path\"\n",
|
| 458 |
+
"vocab_path = \"Specify the path\"\n",
|
| 459 |
+
"\n",
|
| 460 |
+
"# Load the configuration\n",
|
| 461 |
+
"config = BertConfig.from_json_file(config_path)\n",
|
| 462 |
+
"config.num_labels = len(tag2idx) # Set the number of labels as per your tag2idx\n",
|
| 463 |
+
"\n",
|
| 464 |
+
"# Load the model with the config and weights\n",
|
| 465 |
+
"model = BertForTokenClassification(config)\n",
|
| 466 |
+
"model.load_state_dict(torch.load(model_weights_path, map_location=torch.device('cpu')))\n",
|
| 467 |
+
"\n",
|
| 468 |
+
"# Load the tokenizer (if needed)\n",
|
| 469 |
+
"tokenizer = BertTokenizer.from_pretrained(vocab_path)\n",
|
| 470 |
+
"\n",
|
| 471 |
+
"# Set the model to use CPU\n",
|
| 472 |
+
"device = torch.device('cpu')\n",
|
| 473 |
+
"model.to(device)"
|
| 474 |
+
]
|
| 475 |
+
},
|
| 476 |
+
{
|
| 477 |
+
"cell_type": "code",
|
| 478 |
+
"execution_count": 10,
|
| 479 |
+
"metadata": {},
|
| 480 |
+
"outputs": [],
|
| 481 |
+
"source": [
|
| 482 |
+
"def predict_ner_labels(statement, model, tokenizer, tag2name, max_len=60, device='cpu'):\n",
|
| 483 |
+
" # Tokenize and prepare the input\n",
|
| 484 |
+
" tokenized_input = tokenizer.tokenize(statement)\n",
|
| 485 |
+
" tokenized_input = ['[CLS]'] + tokenized_input + ['[SEP]']\n",
|
| 486 |
+
" input_ids = tokenizer.convert_tokens_to_ids(tokenized_input)\n",
|
| 487 |
+
" \n",
|
| 488 |
+
" # Create attention mask\n",
|
| 489 |
+
" attention_mask = [1] * len(input_ids)\n",
|
| 490 |
+
" \n",
|
| 491 |
+
" # Pad input IDs and attention mask\n",
|
| 492 |
+
" padding_length = max_len - len(input_ids)\n",
|
| 493 |
+
" input_ids += [0] * padding_length\n",
|
| 494 |
+
" attention_mask += [0] * padding_length\n",
|
| 495 |
+
" \n",
|
| 496 |
+
" # Convert to tensors\n",
|
| 497 |
+
" input_ids_tensor = torch.tensor([input_ids], dtype=torch.long).to(device)\n",
|
| 498 |
+
" attention_mask_tensor = torch.tensor([attention_mask], dtype=torch.long).to(device)\n",
|
| 499 |
+
" \n",
|
| 500 |
+
" # Set model to evaluation mode and predict\n",
|
| 501 |
+
" model.eval()\n",
|
| 502 |
+
" with torch.no_grad():\n",
|
| 503 |
+
" outputs = model(input_ids_tensor, attention_mask=attention_mask_tensor)\n",
|
| 504 |
+
" logits = outputs[0]\n",
|
| 505 |
+
" predicted_token_ids = torch.argmax(F.log_softmax(logits, dim=2), dim=2).cpu().numpy()\n",
|
| 506 |
+
" \n",
|
| 507 |
+
" # Convert predicted token IDs to labels\n",
|
| 508 |
+
" predicted_labels = [tag2name[id_] for id_ in predicted_token_ids[0]]\n",
|
| 509 |
+
" \n",
|
| 510 |
+
" # Filter out special tokens ([CLS], [SEP], [PAD])\n",
|
| 511 |
+
" filtered_tokens = []\n",
|
| 512 |
+
" filtered_labels = []\n",
|
| 513 |
+
" final_labels = []\n",
|
| 514 |
+
" \n",
|
| 515 |
+
" # For merging subwords\n",
|
| 516 |
+
" current_word = \"\"\n",
|
| 517 |
+
" current_label = \"\"\n",
|
| 518 |
+
"\n",
|
| 519 |
+
" for token, label in zip(tokenized_input, predicted_labels):\n",
|
| 520 |
+
" if token in [\"[CLS]\", \"[SEP]\", \"[PAD]\"]:\n",
|
| 521 |
+
" continue\n",
|
| 522 |
+
" \n",
|
| 523 |
+
" # Check if the token is a subword (starts with '##')\n",
|
| 524 |
+
" if token.startswith(\"##\"):\n",
|
| 525 |
+
" current_word += token[2:] # Append the subword without '##'\n",
|
| 526 |
+
" else:\n",
|
| 527 |
+
" if current_word: # Append previous word with its label\n",
|
| 528 |
+
" filtered_tokens.append(current_word)\n",
|
| 529 |
+
" filtered_labels.append(current_label)\n",
|
| 530 |
+
" current_word = token # Start new word\n",
|
| 531 |
+
" current_label = label\n",
|
| 532 |
+
" \n",
|
| 533 |
+
" # If it's a valid label (not 'X'), set the current label\n",
|
| 534 |
+
" if label != 'X':\n",
|
| 535 |
+
" current_label = label\n",
|
| 536 |
+
" \n",
|
| 537 |
+
" # Append the last word\n",
|
| 538 |
+
" if current_word:\n",
|
| 539 |
+
" filtered_tokens.append(current_word)\n",
|
| 540 |
+
" filtered_labels.append(current_label)\n",
|
| 541 |
+
"\n",
|
| 542 |
+
" # Return token-label pairs\n",
|
| 543 |
+
" return list(zip(filtered_tokens, filtered_labels))\n",
|
| 544 |
+
"\n"
|
| 545 |
+
]
|
| 546 |
+
},
|
| 547 |
+
{
|
| 548 |
+
"cell_type": "code",
|
| 549 |
+
"execution_count": 19,
|
| 550 |
+
"metadata": {},
|
| 551 |
+
"outputs": [
|
| 552 |
+
{
|
| 553 |
+
"name": "stdout",
|
| 554 |
+
"output_type": "stream",
|
| 555 |
+
"text": [
|
| 556 |
+
"i: O\n",
|
| 557 |
+
"need: O\n",
|
| 558 |
+
"an: O\n",
|
| 559 |
+
"update: O\n",
|
| 560 |
+
"on: O\n",
|
| 561 |
+
"the: O\n",
|
| 562 |
+
"verification: O\n",
|
| 563 |
+
"status: O\n",
|
| 564 |
+
"for: O\n",
|
| 565 |
+
"my: O\n",
|
| 566 |
+
"new: O\n",
|
| 567 |
+
"address: O\n",
|
| 568 |
+
"in: O\n",
|
| 569 |
+
"jubilee: B-Address\n",
|
| 570 |
+
"hills: I-Address\n",
|
| 571 |
+
",: I-Address\n",
|
| 572 |
+
"hyderabad: I-Address\n",
|
| 573 |
+
",: O\n",
|
| 574 |
+
"as: O\n",
|
| 575 |
+
"per: O\n",
|
| 576 |
+
"instructions: O\n",
|
| 577 |
+
"from: O\n",
|
| 578 |
+
"the: O\n",
|
| 579 |
+
"finance: B-Team\n",
|
| 580 |
+
"team: I-Team\n",
|
| 581 |
+
"assigned: O\n",
|
| 582 |
+
"by: O\n",
|
| 583 |
+
"priya: B-Person\n",
|
| 584 |
+
".: O\n"
|
| 585 |
+
]
|
| 586 |
+
}
|
| 587 |
+
],
|
| 588 |
+
"source": [
|
| 589 |
+
"statement = \"I need an update on the verification status for my new address in Jubilee Hills, Hyderabad, as per instructions from the Finance Team assigned by Priya.\"\n",
|
| 590 |
+
"predictions = predict_ner_labels(statement, model, tokenizer, tag2name)\n",
|
| 591 |
+
"for token, label in predictions:\n",
|
| 592 |
+
" print(f\"{token}: {label}\")"
|
| 593 |
+
]
|
| 594 |
+
},
|
| 595 |
+
{
|
| 596 |
+
"cell_type": "code",
|
| 597 |
+
"execution_count": null,
|
| 598 |
+
"metadata": {},
|
| 599 |
+
"outputs": [],
|
| 600 |
+
"source": []
|
| 601 |
+
}
|
| 602 |
+
],
|
| 603 |
+
"metadata": {
|
| 604 |
+
"kernelspec": {
|
| 605 |
+
"display_name": "base",
|
| 606 |
+
"language": "python",
|
| 607 |
+
"name": "python3"
|
| 608 |
+
},
|
| 609 |
+
"language_info": {
|
| 610 |
+
"codemirror_mode": {
|
| 611 |
+
"name": "ipython",
|
| 612 |
+
"version": 3
|
| 613 |
+
},
|
| 614 |
+
"file_extension": ".py",
|
| 615 |
+
"mimetype": "text/x-python",
|
| 616 |
+
"name": "python",
|
| 617 |
+
"nbconvert_exporter": "python",
|
| 618 |
+
"pygments_lexer": "ipython3",
|
| 619 |
+
"version": "3.7.4"
|
| 620 |
+
}
|
| 621 |
+
},
|
| 622 |
+
"nbformat": 4,
|
| 623 |
+
"nbformat_minor": 2
|
| 624 |
+
}
|
README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# NER Customer Support Model
|
| 2 |
+
|
| 3 |
+
This project builds and utilizes a Named Entity Recognition (NER) model tailored for customer support interactions. The model uses BERT and focuses on identifying customer-specific entities such as complaints, product names, and appointment information.
|
| 4 |
+
|
| 5 |
+
## Introduction
|
| 6 |
+
|
| 7 |
+
The goal of this NER model is to improve customer support interactions by recognizing specific entities from customer queries. This enables automated systems to efficiently interpret and route customer queries based on recognized entities.
|
| 8 |
+
|
| 9 |
+
## Requirements
|
| 10 |
+
|
| 11 |
+
This project requires Python 3.7.4 and specific libraries listed in the `requirements.txt` file. Notable dependencies include:
|
| 12 |
+
- BERT (using the Transformers library)
|
| 13 |
+
- PyTorch for model training and inference
|
| 14 |
+
- seqeval for NER evaluation
|
| 15 |
+
- pandas and numpy for data handling
|
| 16 |
+
|
| 17 |
+
## Setup
|
| 18 |
+
|
| 19 |
+
1. **Clone the repository and install dependencies**:
|
| 20 |
+
```bash
|
| 21 |
+
pip install -r requirements.txt
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
2. **Download Pre-trained BERT model**:
|
| 25 |
+
Ensure you have a trained BERT model for token classification saved with configuration and weights. The model components should be available as separate files, such as `config.json`, `pytorch_model.bin`, and `vocab.txt`.
|
| 26 |
+
|
| 27 |
+
## Usage
|
| 28 |
+
|
| 29 |
+
1. **Load the Model and Tokenizer**:
|
| 30 |
+
|
| 31 |
+
Specify the paths to the configuration, model weights, and tokenizer files:
|
| 32 |
+
|
| 33 |
+
```python
|
| 34 |
+
from transformers import BertForTokenClassification, BertConfig, BertTokenizer
|
| 35 |
+
import torch
|
| 36 |
+
|
| 37 |
+
# Specify paths
|
| 38 |
+
config_path = "path/to/config.json"
|
| 39 |
+
model_weights_path = "path/to/pytorch_model.bin"
|
| 40 |
+
vocab_path = "path/to/vocab.txt"
|
| 41 |
+
|
| 42 |
+
# Load config and model
|
| 43 |
+
config = BertConfig.from_json_file(config_path)
|
| 44 |
+
model = BertForTokenClassification(config)
|
| 45 |
+
model.load_state_dict(torch.load(model_weights_path, map_location=torch.device('cpu')))
|
| 46 |
+
|
| 47 |
+
# Load tokenizer
|
| 48 |
+
tokenizer = BertTokenizer.from_pretrained(vocab_path)
|
| 49 |
+
|
| 50 |
+
# Set device
|
| 51 |
+
device = torch.device('cpu')
|
| 52 |
+
model.to(device)
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
2. **Tag a Sentence**:
|
| 56 |
+
|
| 57 |
+
After loading the model, pass a sentence to be tagged for entities:
|
| 58 |
+
|
| 59 |
+
```python
|
| 60 |
+
sentence = "Your sample customer query here."
|
| 61 |
+
|
| 62 |
+
# Tokenize and prepare inputs
|
| 63 |
+
inputs = tokenizer(sentence, return_tensors="pt", truncation=True, padding=True)
|
| 64 |
+
inputs = {key: val.to(device) for key, val in inputs.items()}
|
| 65 |
+
|
| 66 |
+
# Predict tags
|
| 67 |
+
with torch.no_grad():
|
| 68 |
+
outputs = model(**inputs)
|
| 69 |
+
logits = outputs.logits
|
| 70 |
+
|
| 71 |
+
# Convert predictions to tags
|
| 72 |
+
predictions = torch.argmax(logits, dim=-1).cpu().numpy()
|
| 73 |
+
tags = [config.id2label[label] for label in predictions[0]]
|
| 74 |
+
|
| 75 |
+
print("Tokens:", tokenizer.tokenize(sentence))
|
| 76 |
+
print("Tags:", tags)
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
## Results
|
| 80 |
+
|
| 81 |
+
The model will output tokens and their corresponding tags for the provided sentence, allowing you to see which entities were recognized.
|
| 82 |
+
|
| 83 |
+
## File Structure
|
| 84 |
+
|
| 85 |
+
- `NER_Customer_final.ipynb`: The main notebook containing data preprocessing, model training, and evaluation.
|
| 86 |
+
- `requirements.txt`: Lists required libraries.
|
| 87 |
+
- `README.md`: This file.
|
| 88 |
+
|
| 89 |
+
## Additional Notes
|
| 90 |
+
|
| 91 |
+
Ensure that GPU support is enabled if available to speed up processing. The code is set to use CPU by default:
|
| 92 |
+
|
| 93 |
+
```python
|
| 94 |
+
device = torch.device('cpu')
|
| 95 |
+
model.to(device)
|
| 96 |
+
```
|
| 97 |
+
|
config.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"BertForMaskedLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_probs_dropout_prob": 0.1,
|
| 6 |
+
"finetuning_task": null,
|
| 7 |
+
"hidden_act": "gelu",
|
| 8 |
+
"hidden_dropout_prob": 0.1,
|
| 9 |
+
"hidden_size": 768,
|
| 10 |
+
"initializer_range": 0.02,
|
| 11 |
+
"intermediate_size": 3072,
|
| 12 |
+
"layer_norm_eps": 1e-12,
|
| 13 |
+
"max_position_embeddings": 512,
|
| 14 |
+
"model_type": "bert",
|
| 15 |
+
"num_attention_heads": 12,
|
| 16 |
+
"num_hidden_layers": 12,
|
| 17 |
+
"num_labels": 36,
|
| 18 |
+
"output_attentions": false,
|
| 19 |
+
"output_hidden_states": false,
|
| 20 |
+
"pad_token_id": 0,
|
| 21 |
+
"pruned_heads": {},
|
| 22 |
+
"torchscript": false,
|
| 23 |
+
"type_vocab_size": 2,
|
| 24 |
+
"vocab_size": 30522
|
| 25 |
+
}
|
model_full_v3.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e48996204b718b626a05012a4e6e9e92eeedee99d629dda11deea09ad5ac34d6
|
| 3 |
+
size 438138609
|
pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2ffe5fb88ff543c79243578f5e27e9588a551aa629889beb59ea06e5da3fa646
|
| 3 |
+
size 438118385
|
pytorch_model_v3.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ff60de4460c85dd1120a7575b62a16deb1478c4f1749afdf4783a2c253e4f76d
|
| 3 |
+
size 438118994
|
requirements.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Specify Python version
|
| 2 |
+
python==3.7.4
|
| 3 |
+
|
| 4 |
+
# Libraries used in the notebook
|
| 5 |
+
pandas
|
| 6 |
+
math
|
| 7 |
+
numpy
|
| 8 |
+
seqeval
|
| 9 |
+
torch
|
| 10 |
+
tqdm
|
| 11 |
+
keras
|
| 12 |
+
transformers
|
vocab.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|