File size: 57,290 Bytes
3ac3892 |
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 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "2a409dd5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From d:\\Anaconda\\Lib\\site-packages\\tf_keras\\src\\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.\n",
"\n",
"Menggunakan perangkat: cuda\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"[I 2025-07-18 06:26:20,055] A new study created in memory with name: no-name-50af0249-7af4-476f-988c-7342adeab58c\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Memulai hyperparameter tuning dengan Optuna...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Some weights of BertForTokenClassification were not initialized from the model checkpoint at indobenchmark/indobert-base-p1 and are newly initialized: ['classifier.bias', 'classifier.weight']\n",
"You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n",
"C:\\Users\\BUDI\\AppData\\Local\\Temp\\ipykernel_6152\\2584540621.py:147: FutureWarning: `tokenizer` is deprecated and will be removed in version 5.0.0 for `Trainer.__init__`. Use `processing_class` instead.\n",
" trainer = Trainer(\n"
]
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='836' max='836' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [836/836 03:00, Epoch 4/4]\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>Precision</th>\n",
" <th>Recall</th>\n",
" <th>F1</th>\n",
" <th>Accuracy</th>\n",
" <th>Per Entity</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>0.124700</td>\n",
" <td>0.166868</td>\n",
" <td>0.748068</td>\n",
" <td>0.731118</td>\n",
" <td>0.739496</td>\n",
" <td>0.945582</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>0.103800</td>\n",
" <td>0.157893</td>\n",
" <td>0.750355</td>\n",
" <td>0.799094</td>\n",
" <td>0.773958</td>\n",
" <td>0.952456</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>0.096100</td>\n",
" <td>0.171932</td>\n",
" <td>0.800613</td>\n",
" <td>0.788520</td>\n",
" <td>0.794521</td>\n",
" <td>0.955606</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4</td>\n",
" <td>0.032800</td>\n",
" <td>0.178615</td>\n",
" <td>0.750704</td>\n",
" <td>0.805136</td>\n",
" <td>0.776968</td>\n",
" <td>0.954031</td>\n",
" <td>{}</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": [
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n"
]
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='27' max='27' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [27/27 00:01]\n",
" </div>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"[I 2025-07-18 06:29:29,091] Trial 0 finished with value: 0.7945205479452055 and parameters: {'learning_rate': 2.3555847899573657e-05, 'batch_size': 8, 'num_epochs': 4}. Best is trial 0 with value: 0.7945205479452055.\n",
"Some weights of BertForTokenClassification were not initialized from the model checkpoint at indobenchmark/indobert-base-p1 and are newly initialized: ['classifier.bias', 'classifier.weight']\n",
"You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n",
"C:\\Users\\BUDI\\AppData\\Local\\Temp\\ipykernel_6152\\2584540621.py:147: FutureWarning: `tokenizer` is deprecated and will be removed in version 5.0.0 for `Trainer.__init__`. Use `processing_class` instead.\n",
" trainer = Trainer(\n"
]
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='1045' max='1045' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [1045/1045 04:05, Epoch 5/5]\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>Precision</th>\n",
" <th>Recall</th>\n",
" <th>F1</th>\n",
" <th>Accuracy</th>\n",
" <th>Per Entity</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>0.123500</td>\n",
" <td>0.163488</td>\n",
" <td>0.728788</td>\n",
" <td>0.726586</td>\n",
" <td>0.727685</td>\n",
" <td>0.945009</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>0.108800</td>\n",
" <td>0.155614</td>\n",
" <td>0.737346</td>\n",
" <td>0.814199</td>\n",
" <td>0.773869</td>\n",
" <td>0.953745</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>0.110300</td>\n",
" <td>0.170470</td>\n",
" <td>0.763314</td>\n",
" <td>0.779456</td>\n",
" <td>0.771300</td>\n",
" <td>0.953172</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4</td>\n",
" <td>0.045800</td>\n",
" <td>0.182373</td>\n",
" <td>0.765557</td>\n",
" <td>0.799094</td>\n",
" <td>0.781966</td>\n",
" <td>0.954031</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>5</td>\n",
" <td>0.022400</td>\n",
" <td>0.191159</td>\n",
" <td>0.758571</td>\n",
" <td>0.802115</td>\n",
" <td>0.779736</td>\n",
" <td>0.953315</td>\n",
" <td>{}</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": [
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n"
]
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='27' max='27' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [27/27 00:01]\n",
" </div>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"[I 2025-07-18 06:33:40,086] Trial 1 finished with value: 0.7819660014781965 and parameters: {'learning_rate': 1.7904807706862636e-05, 'batch_size': 8, 'num_epochs': 5}. Best is trial 0 with value: 0.7945205479452055.\n",
"Some weights of BertForTokenClassification were not initialized from the model checkpoint at indobenchmark/indobert-base-p1 and are newly initialized: ['classifier.bias', 'classifier.weight']\n",
"You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n",
"C:\\Users\\BUDI\\AppData\\Local\\Temp\\ipykernel_6152\\2584540621.py:147: FutureWarning: `tokenizer` is deprecated and will be removed in version 5.0.0 for `Trainer.__init__`. Use `processing_class` instead.\n",
" trainer = Trainer(\n"
]
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='420' max='420' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [420/420 05:47, Epoch 4/4]\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>Precision</th>\n",
" <th>Recall</th>\n",
" <th>F1</th>\n",
" <th>Accuracy</th>\n",
" <th>Per Entity</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>0.138600</td>\n",
" <td>0.185550</td>\n",
" <td>0.738769</td>\n",
" <td>0.670695</td>\n",
" <td>0.703088</td>\n",
" <td>0.942432</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>0.109800</td>\n",
" <td>0.154619</td>\n",
" <td>0.781899</td>\n",
" <td>0.796073</td>\n",
" <td>0.788922</td>\n",
" <td>0.955463</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>0.069800</td>\n",
" <td>0.155078</td>\n",
" <td>0.807750</td>\n",
" <td>0.818731</td>\n",
" <td>0.813203</td>\n",
" <td>0.960332</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4</td>\n",
" <td>0.027200</td>\n",
" <td>0.174292</td>\n",
" <td>0.765292</td>\n",
" <td>0.812689</td>\n",
" <td>0.788278</td>\n",
" <td>0.954747</td>\n",
" <td>{}</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": [
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n"
]
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='14' max='14' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [14/14 00:00]\n",
" </div>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"[I 2025-07-18 06:39:32,835] Trial 2 finished with value: 0.8132033008252062 and parameters: {'learning_rate': 3.672145523121866e-05, 'batch_size': 16, 'num_epochs': 4}. Best is trial 2 with value: 0.8132033008252062.\n",
"Some weights of BertForTokenClassification were not initialized from the model checkpoint at indobenchmark/indobert-base-p1 and are newly initialized: ['classifier.bias', 'classifier.weight']\n",
"You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n",
"C:\\Users\\BUDI\\AppData\\Local\\Temp\\ipykernel_6152\\2584540621.py:147: FutureWarning: `tokenizer` is deprecated and will be removed in version 5.0.0 for `Trainer.__init__`. Use `processing_class` instead.\n",
" trainer = Trainer(\n"
]
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='525' max='525' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [525/525 07:42, Epoch 5/5]\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>Precision</th>\n",
" <th>Recall</th>\n",
" <th>F1</th>\n",
" <th>Accuracy</th>\n",
" <th>Per Entity</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>0.143200</td>\n",
" <td>0.170970</td>\n",
" <td>0.745514</td>\n",
" <td>0.690332</td>\n",
" <td>0.716863</td>\n",
" <td>0.945869</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>0.107300</td>\n",
" <td>0.154406</td>\n",
" <td>0.766141</td>\n",
" <td>0.806647</td>\n",
" <td>0.785872</td>\n",
" <td>0.953029</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>0.075100</td>\n",
" <td>0.158503</td>\n",
" <td>0.795420</td>\n",
" <td>0.787009</td>\n",
" <td>0.791192</td>\n",
" <td>0.956895</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4</td>\n",
" <td>0.025800</td>\n",
" <td>0.179348</td>\n",
" <td>0.764791</td>\n",
" <td>0.800604</td>\n",
" <td>0.782288</td>\n",
" <td>0.954461</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>5</td>\n",
" <td>0.013400</td>\n",
" <td>0.185257</td>\n",
" <td>0.766049</td>\n",
" <td>0.811178</td>\n",
" <td>0.787968</td>\n",
" <td>0.953888</td>\n",
" <td>{}</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": [
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n"
]
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='14' max='14' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [14/14 00:01]\n",
" </div>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"[I 2025-07-18 06:47:22,280] Trial 3 finished with value: 0.7911921032649962 and parameters: {'learning_rate': 3.713773945286763e-05, 'batch_size': 16, 'num_epochs': 5}. Best is trial 2 with value: 0.8132033008252062.\n",
"Some weights of BertForTokenClassification were not initialized from the model checkpoint at indobenchmark/indobert-base-p1 and are newly initialized: ['classifier.bias', 'classifier.weight']\n",
"You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n",
"C:\\Users\\BUDI\\AppData\\Local\\Temp\\ipykernel_6152\\2584540621.py:147: FutureWarning: `tokenizer` is deprecated and will be removed in version 5.0.0 for `Trainer.__init__`. Use `processing_class` instead.\n",
" trainer = Trainer(\n"
]
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='1045' max='1045' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [1045/1045 04:30, Epoch 5/5]\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>Precision</th>\n",
" <th>Recall</th>\n",
" <th>F1</th>\n",
" <th>Accuracy</th>\n",
" <th>Per Entity</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>0.132700</td>\n",
" <td>0.169205</td>\n",
" <td>0.715361</td>\n",
" <td>0.717523</td>\n",
" <td>0.716440</td>\n",
" <td>0.944007</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>0.120000</td>\n",
" <td>0.155390</td>\n",
" <td>0.750700</td>\n",
" <td>0.809668</td>\n",
" <td>0.779070</td>\n",
" <td>0.953458</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>0.136600</td>\n",
" <td>0.163555</td>\n",
" <td>0.761974</td>\n",
" <td>0.793051</td>\n",
" <td>0.777202</td>\n",
" <td>0.954174</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4</td>\n",
" <td>0.067900</td>\n",
" <td>0.172124</td>\n",
" <td>0.766476</td>\n",
" <td>0.808157</td>\n",
" <td>0.786765</td>\n",
" <td>0.953888</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>5</td>\n",
" <td>0.035200</td>\n",
" <td>0.180249</td>\n",
" <td>0.759943</td>\n",
" <td>0.808157</td>\n",
" <td>0.783309</td>\n",
" <td>0.953745</td>\n",
" <td>{}</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": [
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n"
]
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='27' max='27' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [27/27 00:01]\n",
" </div>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"[I 2025-07-18 06:51:59,633] Trial 4 finished with value: 0.7867647058823529 and parameters: {'learning_rate': 1.1923156920458335e-05, 'batch_size': 8, 'num_epochs': 5}. Best is trial 2 with value: 0.8132033008252062.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Hyperparameter terbaik:\n",
"{'learning_rate': 3.672145523121866e-05, 'batch_size': 16, 'num_epochs': 4}\n",
"F1-Score terbaik: 0.8132\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Some weights of BertForTokenClassification were not initialized from the model checkpoint at indobenchmark/indobert-base-p1 and are newly initialized: ['classifier.bias', 'classifier.weight']\n",
"You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n",
"C:\\Users\\BUDI\\AppData\\Local\\Temp\\ipykernel_6152\\2584540621.py:195: FutureWarning: `tokenizer` is deprecated and will be removed in version 5.0.0 for `Trainer.__init__`. Use `processing_class` instead.\n",
" trainer = Trainer(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Memulai pelatihan dengan hyperparameter terbaik...\n"
]
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='420' max='420' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [420/420 07:01, Epoch 4/4]\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>Precision</th>\n",
" <th>Recall</th>\n",
" <th>F1</th>\n",
" <th>Accuracy</th>\n",
" <th>Per Entity</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>0.138600</td>\n",
" <td>0.185550</td>\n",
" <td>0.738769</td>\n",
" <td>0.670695</td>\n",
" <td>0.703088</td>\n",
" <td>0.942432</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>0.109800</td>\n",
" <td>0.154619</td>\n",
" <td>0.781899</td>\n",
" <td>0.796073</td>\n",
" <td>0.788922</td>\n",
" <td>0.955463</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>0.069800</td>\n",
" <td>0.155078</td>\n",
" <td>0.807750</td>\n",
" <td>0.818731</td>\n",
" <td>0.813203</td>\n",
" <td>0.960332</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4</td>\n",
" <td>0.027200</td>\n",
" <td>0.174292</td>\n",
" <td>0.765292</td>\n",
" <td>0.812689</td>\n",
" <td>0.788278</td>\n",
" <td>0.954747</td>\n",
" <td>{}</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": [
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Mengevaluasi model pada data test...\n"
]
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='14' max='14' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [14/14 00:05]\n",
" </div>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval_per_entity\" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.\n",
"Trainer is attempting to log a value of \"{}\" of type <class 'dict'> for key \"eval/per_entity\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Hasil Evaluasi:\n",
"Precision: 0.7528\n",
"Recall: 0.7878\n",
"F1-Score: 0.7699\n",
"Accuracy: 0.9497\n",
"\n",
"Metrik per Entitas:\n",
"\n",
"Model dan tokenizer telah disimpan ke './ner_model'\n",
"\n",
"Contoh Prediksi pada Data Test (5 Sampel):\n",
"\n",
"Sampel 1:\n",
"Tokens: [CLS] joe ##tat ##a hadi ##hard ##aja dan dihadiri oleh rektor undip prof . [SEP]\n",
"True Labels: ['B-PERSON', 'I-PERSON', 'O', 'O', 'O', 'O', 'B-ORGANISATION', 'O', 'O']\n",
"Predicted Labels: ['B-PERSON', 'I-PERSON', 'O', 'O', 'O', 'O', 'B-PLACE', 'O', 'O']\n",
"\n",
"Sampel 2:\n",
"Tokens: [CLS] sejak masih duduk di bangku sekolah tk kevin sudah belajar alat musik piano secara formal dan ketika ia menginjak sekolah smp pemilik nama asli kevin april ##io sum ##aat ##maj ##a ini , mulai belajar menulis lagu sendiri . [SEP]\n",
"True Labels: ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-PERSON', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-PERSON', 'I-PERSON', 'I-PERSON', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']\n",
"Predicted Labels: ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-PERSON', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-PERSON', 'I-PERSON', 'I-PERSON', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']\n",
"\n",
"Sampel 3:\n",
"Tokens: [CLS] pada tanggal 6 februari 1976 , wakil ketua lock ##he ##ed corporation memberitahu subk ##omi ##te senat as bahwa tana ##ka selaku pm telah dibayar ( dis ##ogo ##k ) sebagai ganjaran pembelian pesawat lock ##he ##ed l - 1011 . [SEP]\n",
"True Labels: ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-ORGANISATION', 'I-ORGANISATION', 'O', 'O', 'O', 'B-PLACE', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-ORGANISATION', 'O', 'O', 'O', 'O']\n",
"Predicted Labels: ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-ORGANISATION', 'I-ORGANISATION', 'O', 'O', 'O', 'B-PLACE', 'O', 'B-PERSON', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-ORGANISATION', 'O', 'O', 'O', 'O']\n",
"\n",
"Sampel 4:\n",
"Tokens: [CLS] dengan kondisi alam yang sejuk dan curah hujan yang tinggi maka didaerah tersebut banyak didapati bermacam jenis flora dan fauna seperti : gajah yang di kenal dengan legenda poc ##ut me ##urah ##nya , rusa , harimau , beruang , kancil , babi hutan , tengg ##iling , landak dan ular , juga terdapat berbagai macam jenis burung yang selalu menghiasi kawasan ini . [SEP]\n",
"True Labels: ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']\n",
"Predicted Labels: ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']\n",
"\n",
"Sampel 5:\n",
"Tokens: [CLS] awak pesawat yang terdiri atas pilot ard ##y ted ##jo , kopi ##lot h ribuan dan dua awak lainnya perry reh ##ata dan mei ##nas ##ta segera membuka pintu pesawat dan menurunkan penumpang dengan selamat . tanggal 14 juni 2009 , hari minggu , pukul 09 . 20 , pesawat terbang express air jenis dor ##nie ##r d ##32 ##8 - 100 bernomor badan pk - tx ##n , mengalami kecelakaan saat mendarat . [SEP]\n",
"True Labels: ['O', 'O', 'O', 'O', 'O', 'O', 'B-PERSON', 'I-PERSON', 'O', 'O', 'B-PERSON', 'I-PERSON', 'O', 'O', 'O', 'O', 'B-PERSON', 'I-PERSON', 'O', 'B-PERSON', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-ORGANISATION', 'I-ORGANISATION', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']\n",
"Predicted Labels: ['O', 'O', 'O', 'O', 'O', 'O', 'B-PERSON', 'I-PERSON', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-PERSON', 'I-PERSON', 'O', 'B-PERSON', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']\n",
"\n",
"Analisis Pola Error (Tanggal diprediksi sebagai Lokasi):\n",
"Tidak ditemukan contoh tanggal yang diprediksi sebagai lokasi dalam 100 sampel.\n",
"\n",
"Pertimbangan Keamanan Data, Privasi, dan Etika:\n",
"- Dataset bersumber dari berita publik, tidak mengandung informasi sensitif seperti alamat atau nomor identitas.\n",
"- Nama orang dalam dataset berasal dari media publik, aman untuk digunakan.\n",
"- Dataset mencakup berbagai topik berita, mengurangi risiko bias terhadap entitas tertentu.\n"
]
}
],
"source": [
"import os\n",
"import sys\n",
"import subprocess\n",
"import numpy as np\n",
"from datasets import load_dataset\n",
"from transformers import AutoTokenizer, AutoModelForTokenClassification, DataCollatorForTokenClassification, Trainer, TrainingArguments\n",
"import evaluate\n",
"import torch\n",
"import optuna\n",
"\n",
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
"print(f\"Menggunakan perangkat: {device}\")\n",
"\n",
"# Load dataset\n",
"try:\n",
" dataset = load_dataset(\"indonlp/indonlu\", \"nergrit\", trust_remote_code=True)\n",
"except Exception as e:\n",
" print(f\"Gagal memuat dataset: {e}\")\n",
" sys.exit(1)\n",
"\n",
"# Verify dataset structure\n",
"if \"train\" not in dataset or \"validation\" not in dataset or \"test\" not in dataset:\n",
" print(\"Dataset tidak memiliki split train/validation/test yang diharapkan.\")\n",
" sys.exit(1)\n",
"if \"tokens\" not in dataset[\"train\"].column_names or \"ner_tags\" not in dataset[\"train\"].column_names:\n",
" print(\"Dataset tidak memiliki kolom 'tokens' atau 'ner_tags'.\")\n",
" sys.exit(1)\n",
"\n",
"# Define label list\n",
"try:\n",
" label_list = dataset[\"train\"].features[\"ner_tags\"].feature.names\n",
" label2id = {label: i for i, label in enumerate(label_list)}\n",
" id2label = {i: label for i, label in enumerate(label_list)}\n",
"except Exception as e:\n",
" print(f\"Gagal mendapatkan label: {e}\")\n",
" sys.exit(1)\n",
"\n",
"# Load tokenizer\n",
"try:\n",
" tokenizer = AutoTokenizer.from_pretrained(\"indobenchmark/indobert-base-p1\")\n",
"except Exception as e:\n",
" print(f\"Gagal memuat tokenizer: {e}\")\n",
" sys.exit(1)\n",
"\n",
"# Tokenize and align labels\n",
"def tokenize_and_align_labels(examples):\n",
" tokenized_inputs = tokenizer(examples[\"tokens\"], truncation=True, is_split_into_words=True)\n",
" labels = []\n",
" for i, label in enumerate(examples[\"ner_tags\"]):\n",
" word_ids = tokenized_inputs.word_ids(batch_index=i)\n",
" previous_word_idx = None\n",
" label_ids = []\n",
" for word_idx in word_ids:\n",
" if word_idx is None:\n",
" label_ids.append(-100)\n",
" elif word_idx != previous_word_idx:\n",
" label_ids.append(label[word_idx])\n",
" else:\n",
" label_ids.append(-100)\n",
" previous_word_idx = word_idx\n",
" labels.append(label_ids)\n",
" tokenized_inputs[\"labels\"] = labels\n",
" return tokenized_inputs\n",
"\n",
"# Tokenize dataset\n",
"try:\n",
" tokenized_dataset = dataset.map(tokenize_and_align_labels, batched=True)\n",
"except Exception as e:\n",
" print(f\"Gagal menokenisasi dataset: {e}\")\n",
" sys.exit(1)\n",
"\n",
"# Data collator\n",
"data_collator = DataCollatorForTokenClassification(tokenizer)\n",
"\n",
"# Load evaluation metric\n",
"metric = evaluate.load(\"seqeval\")\n",
"\n",
"# Compute metrics\n",
"def compute_metrics(p):\n",
" predictions, labels = p\n",
" predictions = np.argmax(predictions, axis=2)\n",
" true_labels = [[id2label[l] for l in label if l != -100] for label in labels]\n",
" pred_labels = [[id2label[p] for p, l in zip(prediction, label) if l != -100] for prediction, label in zip(predictions, labels)]\n",
" results = metric.compute(predictions=pred_labels, references=true_labels)\n",
" per_entity = {}\n",
" for entity in [\"PERSON\", \"ORGANISATION\", \"PLACE\", \"DATE\"]:\n",
" if entity.lower() in results:\n",
" per_entity[entity] = {\n",
" \"precision\": results[entity.lower()][\"precision\"],\n",
" \"recall\": results[entity.lower()][\"recall\"],\n",
" \"f1\": results[entity.lower()][\"f1\"],\n",
" }\n",
" return {\n",
" \"precision\": results[\"overall_precision\"],\n",
" \"recall\": results[\"overall_recall\"],\n",
" \"f1\": results[\"overall_f1\"],\n",
" \"accuracy\": results[\"overall_accuracy\"],\n",
" \"per_entity\": per_entity,\n",
" }\n",
"\n",
"# Define objective function for Optuna\n",
"def objective(trial):\n",
" # Define hyperparameter search space\n",
" learning_rate = trial.suggest_float(\"learning_rate\", 1e-5, 5e-5, log=True)\n",
" batch_size = trial.suggest_categorical(\"batch_size\", [8, 16, 32])\n",
" num_epochs = trial.suggest_int(\"num_epochs\", 3, 5)\n",
"\n",
" # Load model for each trial\n",
" model = AutoModelForTokenClassification.from_pretrained(\n",
" \"indobenchmark/indobert-base-p1\",\n",
" num_labels=len(label_list),\n",
" id2label=id2label,\n",
" label2id=label2id\n",
" )\n",
" model.to(device)\n",
"\n",
" # Set training arguments\n",
" training_args = TrainingArguments(\n",
" output_dir=f\"./results_trial_{trial.number}\",\n",
" eval_strategy=\"epoch\",\n",
" learning_rate=learning_rate,\n",
" per_device_train_batch_size=batch_size,\n",
" per_device_eval_batch_size=batch_size,\n",
" num_train_epochs=num_epochs,\n",
" weight_decay=0.01,\n",
" logging_dir=f\"./logs_trial_{trial.number}\",\n",
" logging_steps=10,\n",
" save_strategy=\"epoch\",\n",
" load_best_model_at_end=True,\n",
" metric_for_best_model=\"f1\",\n",
" )\n",
"\n",
" # Initialize Trainer\n",
" trainer = Trainer(\n",
" model=model,\n",
" args=training_args,\n",
" train_dataset=tokenized_dataset[\"train\"],\n",
" eval_dataset=tokenized_dataset[\"validation\"],\n",
" tokenizer=tokenizer,\n",
" data_collator=data_collator,\n",
" compute_metrics=compute_metrics,\n",
" )\n",
"\n",
" # Train and evaluate\n",
" trainer.train()\n",
" eval_results = trainer.evaluate()\n",
" return eval_results[\"eval_f1\"]\n",
"\n",
"# Run Optuna optimization\n",
"print(\"Memulai hyperparameter tuning dengan Optuna...\")\n",
"study = optuna.create_study(direction=\"maximize\")\n",
"study.optimize(objective, n_trials=5) # Adjust n_trials as needed\n",
"print(\"\\nHyperparameter terbaik:\")\n",
"print(study.best_params)\n",
"print(f\"F1-Score terbaik: {study.best_value:.4f}\")\n",
"\n",
"# Train final model with best hyperparameters\n",
"best_params = study.best_params\n",
"model = AutoModelForTokenClassification.from_pretrained(\n",
" \"indobenchmark/indobert-base-p1\",\n",
" num_labels=len(label_list),\n",
" id2label=id2label,\n",
" label2id=label2id\n",
")\n",
"model.to(device)\n",
"\n",
"training_args = TrainingArguments(\n",
" output_dir=\"./results\",\n",
" eval_strategy=\"epoch\",\n",
" learning_rate=best_params[\"learning_rate\"],\n",
" per_device_train_batch_size=best_params[\"batch_size\"],\n",
" per_device_eval_batch_size=best_params[\"batch_size\"],\n",
" num_train_epochs=best_params[\"num_epochs\"],\n",
" weight_decay=0.01,\n",
" logging_dir=\"./logs\",\n",
" logging_steps=10,\n",
" save_strategy=\"epoch\",\n",
" load_best_model_at_end=True,\n",
" metric_for_best_model=\"f1\",\n",
")\n",
"\n",
"trainer = Trainer(\n",
" model=model,\n",
" args=training_args,\n",
" train_dataset=tokenized_dataset[\"train\"],\n",
" eval_dataset=tokenized_dataset[\"validation\"],\n",
" tokenizer=tokenizer,\n",
" data_collator=data_collator,\n",
" compute_metrics=compute_metrics,\n",
")\n",
"\n",
"# Train the model\n",
"print(\"\\nMemulai pelatihan dengan hyperparameter terbaik...\")\n",
"try:\n",
" trainer.train()\n",
"except Exception as e:\n",
" print(f\"Gagal melatih model: {e}\")\n",
" sys.exit(1)\n",
"\n",
"# Evaluate on test set\n",
"print(\"\\nMengevaluasi model pada data test...\")\n",
"try:\n",
" results = trainer.evaluate(tokenized_dataset[\"test\"])\n",
"except Exception as e:\n",
" print(f\"Gagal mengevaluasi model: {e}\")\n",
" sys.exit(1)\n",
"\n",
"# Print evaluation results\n",
"print(\"\\nHasil Evaluasi:\")\n",
"print(f\"Precision: {results['eval_precision']:.4f}\")\n",
"print(f\"Recall: {results['eval_recall']:.4f}\")\n",
"print(f\"F1-Score: {results['eval_f1']:.4f}\")\n",
"print(f\"Accuracy: {results['eval_accuracy']:.4f}\")\n",
"print(\"\\nMetrik per Entitas:\")\n",
"for entity, metrics in results.get(\"eval_per_entity\", {}).items():\n",
" print(f\"{entity}:\")\n",
" print(f\" Precision: {metrics['precision']:.4f}\")\n",
" print(f\" Recall: {metrics['recall']:.4f}\")\n",
" print(f\" F1-Score: {metrics['f1']:.4f}\")\n",
"\n",
"# Save the model\n",
"try:\n",
" model.save_pretrained(\"./ner_model\")\n",
" tokenizer.save_pretrained(\"./ner_model\")\n",
" print(\"\\nModel dan tokenizer telah disimpan ke './ner_model'\")\n",
"except Exception as e:\n",
" print(f\"Gagal menyimpan model: {e}\")\n",
" sys.exit(1)\n",
"\n",
"# Example inference on test samples\n",
"print(\"\\nContoh Prediksi pada Data Test (5 Sampel):\")\n",
"try:\n",
" for i in range(min(5, len(tokenized_dataset[\"test\"]))):\n",
" sample = tokenized_dataset[\"test\"][i]\n",
" input_ids = torch.tensor([sample[\"input_ids\"]], device=device)\n",
" attention_mask = torch.tensor([sample[\"attention_mask\"]], device=device)\n",
" model.eval()\n",
" with torch.no_grad():\n",
" outputs = model(input_ids, attention_mask=attention_mask)\n",
" predictions = outputs.logits.argmax(dim=2)[0].cpu().numpy()\n",
" tokens = tokenizer.convert_ids_to_tokens(sample[\"input_ids\"])\n",
" labels = [id2label[pred] for pred, label in zip(predictions, sample[\"labels\"]) if label != -100]\n",
" true_labels = [id2label[label] for label in sample[\"labels\"] if label != -100]\n",
" print(f\"\\nSampel {i+1}:\")\n",
" print(f\"Tokens: {' '.join(tokens)}\")\n",
" print(f\"True Labels: {true_labels}\")\n",
" print(f\"Predicted Labels: {labels}\")\n",
"except Exception as e:\n",
" print(f\"Gagal melakukan inferensi: {e}\")\n",
" sys.exit(1)\n",
"\n",
"# Analyze error patterns (DATE predicted as LOC)\n",
"print(\"\\nAnalisis Pola Error (Tanggal diprediksi sebagai Lokasi):\")\n",
"found_error = False\n",
"for i in range(min(100, len(tokenized_dataset[\"test\"]))):\n",
" sample = tokenized_dataset[\"test\"][i]\n",
" input_ids = torch.tensor([sample[\"input_ids\"]], device=device)\n",
" attention_mask = torch.tensor([sample[\"attention_mask\"]], device=device)\n",
" with torch.no_grad():\n",
" outputs = model(input_ids, attention_mask=attention_mask)\n",
" predictions = outputs.logits.argmax(dim=2)[0].cpu().numpy()\n",
" true_labels = [id2label[label] for label in sample[\"labels\"] if label != -100]\n",
" pred_labels = [id2label[pred] for pred, label in zip(predictions, sample[\"labels\"]) if label != -100]\n",
" for j, (true, pred) in enumerate(zip(true_labels, pred_labels)):\n",
" if true.startswith(\"B-DATE\") and pred.startswith(\"B-LOC\"):\n",
" tokens = tokenizer.convert_ids_to_tokens(sample[\"input_ids\"])\n",
" print(f\"\\nSampel dengan Error (DATE diprediksi sebagai LOC):\")\n",
" print(f\"Tokens: {' '.join(tokens)}\")\n",
" print(f\"True Labels: {true_labels}\")\n",
" print(f\"Predicted Labels: {pred_labels}\")\n",
" found_error = True\n",
" break\n",
" if found_error:\n",
" break\n",
"if not found_error:\n",
" print(\"Tidak ditemukan contoh tanggal yang diprediksi sebagai lokasi dalam 100 sampel.\")\n",
"\n",
"# Data Security, Privacy, and Ethics\n",
"print(\"\\nPertimbangan Keamanan Data, Privasi, dan Etika:\")\n",
"print(\"- Dataset bersumber dari berita publik, tidak mengandung informasi sensitif seperti alamat atau nomor identitas.\")\n",
"print(\"- Nama orang dalam dataset berasal dari media publik, aman untuk digunakan.\")\n",
"print(\"- Dataset mencakup berbagai topik berita, mengurangi risiko bias terhadap entitas tertentu.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "714cfb72",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "93508875",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|