Spaces:
Sleeping
Sleeping
File size: 15,991 Bytes
2ac7b58 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "8495bede-ab8f-416b-b5f2-6a76b1e63935",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"D:\\Projects\\LLMs\\venv\\lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"from tqdm import tqdm\n",
"from sentence_transformers import SentenceTransformer, util"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "2b8cae6d-547b-4018-9f68-b0a45284b4b4",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# model = SentenceTransformer('DMetaSoul/sbert-chinese-general-v2')\n",
"model = SentenceTransformer('TintinMeimei/menglang_yongtulv_aimatch_v1')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d3907a6f-f8ab-40fe-8702-c8cb81e189c6",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def sim(text1, text2):\n",
" emb1 = model.encode(text1, convert_to_tensor=True)\n",
" emb2 = model.encode(text2, convert_to_tensor=True)\n",
" score = util.cos_sim(emb1, emb2)\n",
" return score"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "3cec9f05-4ea9-46f8-a393-950c67a0150a",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"text1 = 'ๆๆบ็ฉบ่ฐ'\n",
"# text2 = '1.1.11 ้ซๆ่่ฝๅฎถ็จ็ตๅจๅถ้ \\nๅ
ๆฌ่่ฝๅๆฟ้ด็ฉบ่ฐๅจใ็ฉบ่ฐๆบ็ปใ็ตๅฐ็ฎฑใ็ตๅจๆด่กฃๆบใๅนณๆฟ็ต่งๆบใ็ต้ฃๆ็ญๅฎถ็จ็ตๅจๅถ้ ใๆฟ้ด็ฉบๆฐ่ฐ่ๅจ่ฝๆไผไบใๆฟ้ด็ฉบๆฐ่ฐ่ๅจ่ฝๆ้ๅฎๅผๅ่ฝๆ็ญ็บงใ๏ผGB 12021.3๏ผๆ ๅ1็บง่ฝๆๆฐดๅนณ๏ผ่ฝฌ้ๅฏๆงๅๆฟ้ด็ฉบๆฐ่ฐ่ๅจ่ฝๆไผไบใ่ฝฌ้ๅฏๆงๅๆฟ้ด็ฉบๆฐ่ฐ่ๅจ่ฝๆ้ๅฎๅผๅ่ฝๆ็ญ็บงใ๏ผGB 21455๏ผๆ ๅ1็บง่ฝๆๆฐดๅนณ๏ผๅค่ๅผ็ฉบ่ฐ๏ผ็ญๆณต๏ผๆบ็ป่ฝๆๆฏไผไบใๅค่ๅผ็ฉบ่ฐ๏ผ็ญๆณต๏ผๆบ็ป่ฝๆ้ๅฎๅผๅ่ฝๆบๆ็็ญ็บงใ๏ผGB 21454๏ผๆ ๅ1็บง่ฝๆๆฐดๅนณ๏ผๅฎถ็จ็ตๅฐ็ฎฑ่ฝๆไผไบใๅฎถ็จ็ตๅฐ็ฎฑ่็ต้้ๅฎๅผๅ่ฝๆ็ญ็บงใ๏ผGB 12021.2๏ผๆ ๅ1็บง่ฝๆๆฐดๅนณ๏ผ็ตๅจๆด่กฃๆบ่ฝๆไผไบใ็ตๅจๆด่กฃๆบ่ฝๆๆฐดๆ้ๅฎๅผๅ็ญ็บงใ๏ผGB 12021.4๏ผๆ ๅ1็บง่ฝๆๆฐดๅนณ๏ผ็ต้ฅญ็
ฒ่ฝๆไผไบใ็ต้ฅญ้
่ฝๆ้ๅฎๅผๅ่ฝๆ็ญ็บงใ๏ผGB 12021.6๏ผๆ ๅ1็บง่ฝๆๆฐดๅนณ๏ผๅนณๆฟ็ต่งๆบ่ฝๆไผไบใๅนณๆฟ็ต่ง่ฝๆ้ๅฎๅผๅ่ฝๆ็ญ็บงใ๏ผGB 24850๏ผๆ ๅ1็บง่ฝๆๆฐดๅนณ๏ผไบคๆต็ต้ฃๆ่ฝๆไผไบใไบคๆต็ต้ฃๆ่ฝๆ้ๅฎๅผๅ่ฝๆ็ญ็บงใ๏ผGB 12021.9๏ผๆ ๅ1็บง่ฝๆๆฐดๅนณใๅ
ถไป้ซๆ่่ฝๅฎถ็จ็ตๅจ่ฝๆๅไผไบ็ธๅบๅฝๅฎถๅผบๅถๆงๆ ๅ1็บง่ฝๆๆฐดๅนณใ'\n",
"# text2 = 'ๅ
ๆฌ่่ฝๆณตใ่่ฝๅ็็ฉบๅนฒ็ฅ่ฎพๅคใ่่ฝๅ็็ฉบ็็ญ่ฎพๅคๅถ้ ใๆธ
ๆฐด็ฆปๅฟๆณต่ฝๆๆๆ ไผไบใๆธ
ๆฐด็ฆปๅฟๆณต่ฝๆ้ๅฎๅผๅ่่ฝ่ฏไปทๅผใ๏ผGB 19762๏ผๆ ๅไธญ่่ฝ่ฏไปทๅผๆฐดๅนณ๏ผ็ณๆฒนๅๅทฅ็ฆปๅฟๆณต่ฝๆไผไบใ็ณๆฒนๅๅทฅ็ฆปๅฟๆณต่ฝๆ้ๅฎๅผๅ่ฝๆ็ญ็บงใ๏ผGB 32284๏ผๆ ๅไธญ1็บง่ฝๆๆฐดๅนณ๏ผๆฝๆฐด็ตๆณต่ฝๆไผไบใไบ็จๆฝๆฐด็ตๆณต่ฝๆ้ๅฎๅผๅ่ฝๆ็ญ็บงใ๏ผGB 32030๏ผใใๅฐๅๆฝๆฐด็ตๆณต่ฝๆ้ๅฎๅผๅ่ฝๆ็ญ็บงใ๏ผGB 32029๏ผใใๆฑกๆฐดๆฑก็ฉๆฝๆฐด็ตๆณต่ฝๆ้ๅฎๅผๅ่ฝๆ็ญ็บงใ๏ผGB 32031๏ผๆ ๅไธญ1็บง่ฝๆๆฐดๅนณใ'\n",
"text2 = '้่่ฟๆ'"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "d570bf57-2518-4306-a7ae-712e81199460",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[-0.5000]], device='cuda:0')"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sim(text1, text2)"
]
},
{
"cell_type": "markdown",
"id": "040cc794-9bb0-4c22-986c-933ca55ee637",
"metadata": {},
"source": [
"### Process Data"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d46e4e74-f7c2-4339-b009-4ba77f1b2f9a",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>X1</th>\n",
" <th>X2</th>\n",
" <th>Y</th>\n",
" <th>Split</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>ไธญๆฐๅถ่ฏๅ็ฉบ่ฐๆซ็ซฏ้ๅ้ฃ็ณป็ปๆน้ -่ฏขไปทๅ
ฌ็คบ</td>\n",
" <td>1.1.11 ้ซๆ่่ฝๅฎถ็จ็ตๅจๅถ้ \\nๅ
ๆฌ่่ฝๅๆฟ้ด็ฉบ่ฐๅจใ็ฉบ่ฐๆบ็ปใ็ตๅฐ็ฎฑใ็ตๅจๆด่กฃๆบใๅนณ...</td>\n",
" <td>1</td>\n",
" <td>train</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>ไธญๆฐๅถ่ฏๅ็ฉบ่ฐๆซ็ซฏ้ๅ้ฃ็ณป็ปๆน้ -่ฏขไปทๅ
ฌ็คบ</td>\n",
" <td>1.5.1 ้
็๏ผ็ช็๏ผ่่ฝๆน้ ๅ่ฝๆๆๅ\\nๅ
ๆฌ็็
ค้
็โไปฅๅคงไปฃๅฐโ๏ผ้็จๅ
่ฟ็็
ค้
็ใ่...</td>\n",
" <td>0</td>\n",
" <td>train</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" X1 X2 \\\n",
"0 ไธญๆฐๅถ่ฏๅ็ฉบ่ฐๆซ็ซฏ้ๅ้ฃ็ณป็ปๆน้ -่ฏขไปทๅ
ฌ็คบ 1.1.11 ้ซๆ่่ฝๅฎถ็จ็ตๅจๅถ้ \\nๅ
ๆฌ่่ฝๅๆฟ้ด็ฉบ่ฐๅจใ็ฉบ่ฐๆบ็ปใ็ตๅฐ็ฎฑใ็ตๅจๆด่กฃๆบใๅนณ... \n",
"1 ไธญๆฐๅถ่ฏๅ็ฉบ่ฐๆซ็ซฏ้ๅ้ฃ็ณป็ปๆน้ -่ฏขไปทๅ
ฌ็คบ 1.5.1 ้
็๏ผ็ช็๏ผ่่ฝๆน้ ๅ่ฝๆๆๅ\\nๅ
ๆฌ็็
ค้
็โไปฅๅคงไปฃๅฐโ๏ผ้็จๅ
่ฟ็็
ค้
็ใ่... \n",
"\n",
" Y Split \n",
"0 1 train \n",
"1 0 train "
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"df_data = pd.read_excel('AIๅน้
็ฎๆณๆ ทๆฌ.xlsx', sheet_name='Sheet1', dtype=str)\n",
"df_data.head(2)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "673ce0e0-2801-4bb3-8e5d-5c4aff3ac725",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\vermouth\\AppData\\Local\\Temp\\ipykernel_22160\\3257358016.py:1: FutureWarning: Using short name for 'orient' is deprecated. Only the options: ('dict', list, 'series', 'split', 'records', 'index') will be used in a future version. Use one of the above to silence this warning.\n",
" train_data = [item for item in df_data.to_dict(orient='record') if item['Split']=='train']\n",
"C:\\Users\\vermouth\\AppData\\Local\\Temp\\ipykernel_22160\\3257358016.py:2: FutureWarning: Using short name for 'orient' is deprecated. Only the options: ('dict', list, 'series', 'split', 'records', 'index') will be used in a future version. Use one of the above to silence this warning.\n",
" eval_data = [item for item in df_data.to_dict(orient='record') if item['Split']=='eval']\n",
"C:\\Users\\vermouth\\AppData\\Local\\Temp\\ipykernel_22160\\3257358016.py:3: FutureWarning: Using short name for 'orient' is deprecated. Only the options: ('dict', list, 'series', 'split', 'records', 'index') will be used in a future version. Use one of the above to silence this warning.\n",
" test_data = [item for item in df_data.to_dict(orient='record') if item['Split']=='test']\n"
]
}
],
"source": [
"train_data = [item for item in df_data.to_dict(orient='record') if item['Split']=='train']\n",
"eval_data = [item for item in df_data.to_dict(orient='record') if item['Split']=='eval']\n",
"test_data = [item for item in df_data.to_dict(orient='record') if item['Split']=='test']"
]
},
{
"cell_type": "markdown",
"id": "5037803d-980d-48a1-a61d-528bb9508ce0",
"metadata": {},
"source": [
"### Model 1 - Fine tune a Sentence Transformer"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "773429e9-57ce-418f-ad44-3c35d1b31a74",
"metadata": {},
"outputs": [],
"source": [
"# from sentence_transformers import InputExample, losses\n",
"# from torch.utils.data import DataLoader\n",
"\n",
"# # Prepare data\n",
"# train_data_sbert = []\n",
"# eval_data_sbert = []\n",
"# test_data_sbert = []\n",
"\n",
"# for item in train_data:\n",
"# label = 1.0 if float(item.get('Y')) == 1 else -1.0\n",
"# train_data_sbert.append(InputExample(texts=[item.get('X1'), item.get('X2')], label=label))\n",
"# train_dataloader = DataLoader(train_data_sbert, shuffle=True, batch_size=2)\n",
"# train_loss = losses.CosineSimilarityLoss(model)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "ec1b68cb-bec3-4896-b196-ec31b1132ad1",
"metadata": {},
"outputs": [],
"source": [
"# from sentence_transformers import evaluation\n",
"# evaluator = evaluation.EmbeddingSimilarityEvaluator([item.get('X1') for item in eval_data], [item.get('X2') for item in eval_data], [1.0 if float(item.get('Y'))==1 else -1.0 for item in eval_data])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "7c05c6ef-c5e7-416b-b797-9f8735ae5436",
"metadata": {},
"outputs": [],
"source": [
"# model.fit(train_objectives=[(train_dataloader, train_loss)], epochs=1, warmup_steps=100, evaluator=evaluator, evaluation_steps=500)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "7de1e5f0-4b83-4d34-8385-77cdaa0ef08f",
"metadata": {},
"outputs": [],
"source": [
"# model.save('./tmp_model')"
]
},
{
"cell_type": "markdown",
"id": "fdd686b1-c654-4135-8989-05f23c914afa",
"metadata": {
"tags": []
},
"source": [
"### Model 2 - No Fine Tune + Threshold Tuning"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "a0247889-577d-4a92-8c0f-9c923748df93",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def sim(text1, text2):\n",
" emb1 = model.encode(text1, convert_to_tensor=True)\n",
" emb2 = model.encode(text2, convert_to_tensor=True)\n",
" score = util.cos_sim(emb1, emb2)\n",
" return score\n",
"\n",
"def _acc_thres(scores, thres):\n",
" correct = 0\n",
" total = len(scores)\n",
" for score, truth in scores:\n",
" truth = float(truth)\n",
" pred = 1 if score >= thres else 0\n",
" if pred == truth:\n",
" correct += 1\n",
" return round(correct/total, 3)\n",
"\n",
"def model_train(train_data, eval_data):\n",
" score_train = []\n",
" score_eval = []\n",
" for item in tqdm(train_data):\n",
" score = sim(item['X1'], item['X2'])\n",
" score_train.append((score, item['Y']))\n",
" for item in tqdm(eval_data):\n",
" score = sim(item['X1'], item['X2'])\n",
" score_eval.append((score, item['Y']))\n",
" # find threshold that minize train error\n",
" score_train = sorted(score_train, reverse=True)\n",
" win_acc = -1\n",
" win_thres = -1\n",
" for thres in range(5, 100, 5):\n",
" thres = thres*0.01\n",
" acc = _acc_thres(score_train, thres)\n",
" if acc > win_acc:\n",
" win_acc = acc\n",
" win_thres = thres\n",
" eval_acc = _acc_thres(score_eval, win_thres)\n",
" return {'thres': win_thres, 'train_accuracy': win_acc, 'eval_accuracy': eval_acc}"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "4e943ef9-ad40-494e-9d53-db9ccbf48bb4",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ| 12256/12256 [13:54<00:00, 14.69it/s]\n",
"100%|โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ| 4248/4248 [04:44<00:00, 14.94it/s]\n"
]
}
],
"source": [
"r = model_train(train_data, eval_data)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "9cd38cc9-fe71-45b9-a22e-977a2e787fb5",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"{'thres': 0.25, 'train_accuracy': 0.831, 'eval_accuracy': 0.816}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "53622ff1-7465-4663-a9f0-0c18df37b93e",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ| 4468/4468 [04:58<00:00, 14.98it/s]\n"
]
}
],
"source": [
"score_test = []\n",
"for item in tqdm(test_data):\n",
" score = sim(item['X1'], item['X2'])\n",
" score_test.append((score, item['Y']))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "47411f71-c774-4274-a1af-2a128589b559",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"0.815"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"_acc_thres(score_test, r['thres'])\n",
"#_acc_thres(score_test, 0.25)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "59b741bc-7a20-4ed0-bc9d-b82ec3edff34",
"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.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|