zaidmehdi commited on
Commit
ba240c4
·
1 Parent(s): 76f50ce

trained logistic regression

Browse files
Files changed (1) hide show
  1. src/classifier.ipynb +111 -11
src/classifier.ipynb CHANGED
@@ -10,24 +10,18 @@
10
  },
11
  {
12
  "cell_type": "code",
13
- "execution_count": 1,
14
  "metadata": {},
15
- "outputs": [
16
- {
17
- "name": "stderr",
18
- "output_type": "stream",
19
- "text": [
20
- "/home/mehdi/miniconda3/envs/adc/lib/python3.11/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",
21
- " from .autonotebook import tqdm as notebook_tqdm\n"
22
- ]
23
- }
24
- ],
25
  "source": [
26
  "import pickle\n",
27
  "\n",
28
  "from datasets import DatasetDict, Dataset\n",
29
  "import matplotlib.pyplot as plt\n",
 
30
  "import pandas as pd\n",
 
 
31
  "import torch\n",
32
  "from transformers import AutoModel, AutoTokenizer"
33
  ]
@@ -488,6 +482,13 @@
488
  "### 2.2 Feature Extraction"
489
  ]
490
  },
 
 
 
 
 
 
 
491
  {
492
  "cell_type": "code",
493
  "execution_count": 13,
@@ -538,6 +539,105 @@
538
  "### 2.3 Model Training"
539
  ]
540
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
541
  {
542
  "cell_type": "markdown",
543
  "metadata": {},
 
10
  },
11
  {
12
  "cell_type": "code",
13
+ "execution_count": 30,
14
  "metadata": {},
15
+ "outputs": [],
 
 
 
 
 
 
 
 
 
16
  "source": [
17
  "import pickle\n",
18
  "\n",
19
  "from datasets import DatasetDict, Dataset\n",
20
  "import matplotlib.pyplot as plt\n",
21
+ "import numpy as np\n",
22
  "import pandas as pd\n",
23
+ "from sklearn.ensemble import RandomForestClassifier\n",
24
+ "from sklearn.linear_model import LogisticRegression\n",
25
  "import torch\n",
26
  "from transformers import AutoModel, AutoTokenizer"
27
  ]
 
482
  "### 2.2 Feature Extraction"
483
  ]
484
  },
485
+ {
486
+ "cell_type": "markdown",
487
+ "metadata": {},
488
+ "source": [
489
+ "Now, we will extract the output of the last hidden layer of AraBART, and use those embeddings as the features of our classifier."
490
+ ]
491
+ },
492
  {
493
  "cell_type": "code",
494
  "execution_count": 13,
 
539
  "### 2.3 Model Training"
540
  ]
541
  },
542
+ {
543
+ "cell_type": "markdown",
544
+ "metadata": {},
545
+ "source": [
546
+ "Now, we only need to convert the data into numpy arrays, and we are ready to train the models."
547
+ ]
548
+ },
549
+ {
550
+ "cell_type": "code",
551
+ "execution_count": 28,
552
+ "metadata": {},
553
+ "outputs": [
554
+ {
555
+ "data": {
556
+ "text/plain": [
557
+ "((21000, 768), (21000,))"
558
+ ]
559
+ },
560
+ "execution_count": 28,
561
+ "metadata": {},
562
+ "output_type": "execute_result"
563
+ }
564
+ ],
565
+ "source": [
566
+ "X_train = np.array(data_hidden[\"train\"][\"hidden_state\"])\n",
567
+ "X_test = np.array(data_hidden[\"test\"][\"hidden_state\"])\n",
568
+ "y_train = np.array(data_hidden[\"train\"][\"label\"])\n",
569
+ "y_test = np.array(data_hidden[\"test\"][\"label\"])\n",
570
+ "\n",
571
+ "X_train.shape, y_train.shape"
572
+ ]
573
+ },
574
+ {
575
+ "cell_type": "markdown",
576
+ "metadata": {},
577
+ "source": [
578
+ "We can try different models."
579
+ ]
580
+ },
581
+ {
582
+ "cell_type": "markdown",
583
+ "metadata": {},
584
+ "source": [
585
+ "#### 2.3.1 Logistic Regression "
586
+ ]
587
+ },
588
+ {
589
+ "cell_type": "code",
590
+ "execution_count": 32,
591
+ "metadata": {},
592
+ "outputs": [
593
+ {
594
+ "data": {
595
+ "text/html": [
596
+ "<style>#sk-container-id-2 {color: black;}#sk-container-id-2 pre{padding: 0;}#sk-container-id-2 div.sk-toggleable {background-color: white;}#sk-container-id-2 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-2 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-2 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-2 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-2 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-2 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-2 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-2 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-2 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-2 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-2 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-2 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-2 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-2 div.sk-item {position: relative;z-index: 1;}#sk-container-id-2 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-2 div.sk-item::before, #sk-container-id-2 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-2 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-2 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-2 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-2 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-2 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-2 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-2 div.sk-label-container {text-align: center;}#sk-container-id-2 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-2 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-2\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>LogisticRegression(class_weight=&#x27;balanced&#x27;, max_iter=1000,\n",
597
+ " multi_class=&#x27;multinomial&#x27;, random_state=2024)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" checked><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">LogisticRegression</label><div class=\"sk-toggleable__content\"><pre>LogisticRegression(class_weight=&#x27;balanced&#x27;, max_iter=1000,\n",
598
+ " multi_class=&#x27;multinomial&#x27;, random_state=2024)</pre></div></div></div></div></div>"
599
+ ],
600
+ "text/plain": [
601
+ "LogisticRegression(class_weight='balanced', max_iter=1000,\n",
602
+ " multi_class='multinomial', random_state=2024)"
603
+ ]
604
+ },
605
+ "execution_count": 32,
606
+ "metadata": {},
607
+ "output_type": "execute_result"
608
+ }
609
+ ],
610
+ "source": [
611
+ "lr_model = LogisticRegression(multi_class='multinomial', \n",
612
+ " class_weight=\"balanced\", \n",
613
+ " max_iter=1000, \n",
614
+ " random_state=2024)\n",
615
+ "lr_model.fit(X_train, y_train)"
616
+ ]
617
+ },
618
+ {
619
+ "cell_type": "markdown",
620
+ "metadata": {},
621
+ "source": [
622
+ "#### 2.3.2 Random Forest"
623
+ ]
624
+ },
625
+ {
626
+ "cell_type": "markdown",
627
+ "metadata": {},
628
+ "source": [
629
+ "For the Random forest, we can do a grid search to optimize the hyperparameters. We will use a 5-fold cross validation strategy."
630
+ ]
631
+ },
632
+ {
633
+ "cell_type": "code",
634
+ "execution_count": null,
635
+ "metadata": {},
636
+ "outputs": [],
637
+ "source": [
638
+ "rf_model = RandomForestClassifier()\n"
639
+ ]
640
+ },
641
  {
642
  "cell_type": "markdown",
643
  "metadata": {},