{"id":26361667,"url":"https://github.com/prashuk/nlp-toxic-comment-classification","last_synced_at":"2026-05-10T16:03:39.677Z","repository":{"id":37229826,"uuid":"224037082","full_name":"prashuk/NLP-Toxic-Comment-Classification","owner":"prashuk","description":"Natural Language Processing Project","archived":false,"fork":false,"pushed_at":"2022-11-21T21:32:57.000Z","size":525,"stargazers_count":2,"open_issues_count":12,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-03-04T13:28:28.791Z","etag":null,"topics":["nlp-machine-learning","python"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prashuk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-25T20:39:29.000Z","updated_at":"2020-10-29T19:07:17.000Z","dependencies_parsed_at":"2023-01-20T21:02:21.161Z","dependency_job_id":null,"html_url":"https://github.com/prashuk/NLP-Toxic-Comment-Classification","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prashuk%2FNLP-Toxic-Comment-Classification","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prashuk%2FNLP-Toxic-Comment-Classification/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prashuk%2FNLP-Toxic-Comment-Classification/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prashuk%2FNLP-Toxic-Comment-Classification/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prashuk","download_url":"https://codeload.github.com/prashuk/NLP-Toxic-Comment-Classification/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243909470,"owners_count":20367534,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nlp-machine-learning","python"],"created_at":"2025-03-16T17:39:46.722Z","updated_at":"2026-05-10T16:03:39.604Z","avatar_url":"https://github.com/prashuk.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ToxicCommentClassification\nAnalysing and desinginh various neural model over the Toxic Comment Classification Dataset provided by Kaggle\n\n\n## The Problem\n\n  Examining things that a person care about can be troublesome. The risk of maltreatment and badgering on the web implies that numerous individuals quit conveying everything that needs to be conveyed and abandon the media platform. Many social media sites struggle to effectively facilitate conversations, leading many communities to limit or completely shut down user comments.\n\n  The Conversation AI group, an exploration activity established by Jigsaw and Google (both a piece of Alphabet) are chipping away at apparatuses to help improve the online discussion.\n\n  S1One zone of center is the investigation of negative online practices, as dangerous remarks (for example remarks that are impolite, discourteous or generally prone to make somebody leave a talk).\n\n  So far they\u0026#39;ve assembled a scope of openly accessible models served through the Perspective API, including poisonous quality. In any case, the present models still make blunders, and they don\u0026#39;t enable clients to choose which kinds of danger they\u0026#39;re keen on finding (for example a few stages might approve of foulness, however not with different kinds of poisonous substance).\n\n## Methodology\n\n  In this section we study baseline methods for the above mentioned common challenges. Further, we propose our Ensembled Sequential learning architecture. Its goal is to minimize errors by detecting optimal methods for a given comment.\n\n  We applied 8 model design with different accuracy.\n\n  a. LSTM model using Glove word vector\n  b. 3 layer deeper LSTM model using Glove word vector\n  c. ResNet of depth 3 in LSTM model using Glove word vector\n  d. Bidirectional LSTM model using Glove word vector\n  e. 3 layer deeper Bidirectional LSTM model using Glove word vector\n  f. ResNet of depth 3 in Bidirectional LSTM model using Glove word vector\n  g. Bidirectional LSTM model with character encodings\n  h. Bidirectional LSTM model using Glove word vector and character encodings\n\n## Data Annotations\n\n**Data Set in use: Wikipedia Talk Pages dataset**\n\nThe dataset is under [CC0](https://creativecommons.org/share-your-work/public-domain/cc0/), with the underlying comment text being governed by [Wikipedia\u0026#39;s CC-SA-3.0](https://creativecommons.org/licenses/by-sa/3.0/)\n\nWe analyze a dataset published by Google Jigsaw in December 2017 over the course of the \u0026#39;Toxic Comment Classification Challenge\u0026#39; on Kaggle. It includes 223,549 annotated user comments collected from Wikipedia talk pages and is the largest publicly available for the task. These comments were annotated by human raters with the six labels:-\n\n- toxic\n- severe\\_toxic\n- bscene\n- threat\n- insult\n- Identity\\_hate\n\nComments can be associated with multiple classes at once, which frames the task as a multi-label classification problem. Jigsaw has not published official definitions for the six classes. But they do state that they defined a toxic comment as \u0026quot;a rude, disrespectful, or unreasonable comment that is likely to make you leave a discussion\u0026quot;.\n\n## Implementation\n\n  ### a.  **Single layer LSTM model using Glove vector**\n\n  Our LSTM model takes a sequence of words as input. An embedding layer transforms one-hot-encoded words to dense vector representations and a spatial dropout, which randomly masks 10% of the input words, makes the network more robust.\n\n  To process the sequence of word embeddings, we use an LSTM layer with 128 units, followed by a dropout of 10%. Finally, a dense layer with a sigmoid activation makes the prediction for the multi-label classification and a dense layer with softmax activation makes the prediction for the multi-class classification.\n\n\n\n  ### b. **Single layer Bidirectional LSTM using Glove vector**\n\n  Bidirectional RNNs can compensate certain errors on long range dependencies. In contrast to the standard LSTM model, the bidirectional LSTM model uses two LSTM layers that process the input sequence in opposite directions. Thereby, the input sequence is processed with correct and reverse order of words. The outputs of these two layers are averaged.\n\n\n\n  ### c. **Three Layer Deeper LSTM model**\n\n  Sometimes going deeper will help us finding pattern which are more complex. Here we went by using same LSTM layers three times. Each layer of 100 units and changing the direction of the LTM layers\n\n \n   ### d.  **Three Layer Deeper Bidirectional LSTM model**\n\n   Here we went deeper in the Bi LSTM model and try seeing what models learns from deeper architecture. We stacked 3 BiLSTM layers each of 100 units.\n\n\n\n  ### e. **ResNet of depth 3 in LSTM model using Glove word vector**\n\n   The problem with the deeper models are that they lose the information while going deeper, we call it gradient loss. To overcome this property we implemented the ResNet Structure. Here we use 1st layers LSTM result and concatenate with 3rd layer LSTM.\n\n\n  ### f. **ResNet of depth 3 in Bidirectional LSTM model using Glove word vector**\n\n   We did similar approach as the model 5 in this model, instead of LSTM we used Bi LSTMs.\n\n  ### g. **Bidirectional LSTM model with character encodings**\n\n  The reason we switched the input was a lot of words in the corpora were not present in the Glove Word vector. Many words were misspelled due to a type or genuinely done to abusive the targeted people and not get detected by a basic regular Expression.\n\n\n  ### h.  **Bidirectional LSTM model using Glove word vector and character encodings**\n\n  Glove vectors store more information about the words and and the character sequence also helps us. So we concatenated both vectors and use them as input. This resulted in best model of the all above models.\n\n\n## **Experimental Results**\n\n  The dataset features an unbalanced class distribution. 201,081 samples fall under the majority \u0026#39;clear\u0026#39; class matching none of the six categories, whereas 22,468 samples belong to at least one of the other classes. While the \u0026#39;toxic\u0026#39; class includes 9.6% of the samples, only 0.3% are labeled as \u0026#39;threat\u0026#39;, marking the smallest class.\n\n  Comments were collected from the English Wikipedia and are mostly written in English with some outliers, e.g., in Arabic, Chinese or German language. The domain covered is not strictly locatable, due to various article topics being discussed. Still it is possible to apply a simple categorization of comments as follows:\n\n  1. \u0026#39;community-related\u0026#39;: Example: \u0026quot;If you continue to vandalize Wikipedia, you will be blocked from editing.\u0026quot;\n  2. \u0026#39;article-related\u0026#39;: Example: \u0026quot;Dark Jedi Miraluka from the MidRim world of Katarr, Visas Marr is the lone surviving member of her species.\u0026quot;\n  3. \u0026#39;off-topic\u0026#39;: Example: \u0026quot;== I hate how my life goes today == Just kill me now.\u0026quot;\n\n  Common Challenges We observe these common challenges for Natural Language Processing in the datasets:\n\n  1. Out-of-vocabulary words. A common problem for the task is the occurrence of words that are not present in the training data. These words include slang or misspellings, but also intentionally obfuscated content.\n  2. Long-Range Dependencies. The toxicity of a comment often depends on expressions made in early parts of the comment. This is especially problematic for longer comments (\\\u0026gt;50 words) where the influence of earlier parts on the result can vanish.\n  3. Misspelled -words . We observed that many words are spelled incorrectly. The reason being the data collected may be of a user not using English as their first language. We also observed that many abusive words we intentionally misspelled for the naive regular expression algorithm to fail. Some of them were basic typing typos.\n\n  We went on to try different models which could fit our dataset. We used the LSTM and Bi LSTM layers and also tried the ResNet Structure.\n\n  For input we used Glove 50d vector and also used character sequence as input. The best model we got was for model using single Bidirectional Layer and input as glove vector and character sequence vectors.\n\n  \n  ### 1.  **LSTM model using Glove word vector**\n\n    Train on 127656 samples, validate on 31915 samples\n\n    Epoch 1/3\n\n    127656/127656 [==============================] - 503s 4ms/step - loss: 0.0671 - acc: 0.9776 - val\\_loss: 0.0490 - val\\_acc: 0.9816\n\n    Epoch 2/3\n\n    127656/127656 [==============================] - 500s 4ms/step - loss: 0.0471 - acc: 0.9823 - val\\_loss: 0.0472 - val\\_acc: 0.9825\n\n    Epoch 3/3\n\n    127656/127656 [==============================] - 491s 4ms/step - loss: 0.0426 - acc: 0.9836 - val\\_loss: 0.0463 - val\\_acc: 0.9827\n\n\n\n  ### 2. **3 layer deeper LSTM model using Glove word vector**\n\n    Train on 127656 samples, validate on 31915 samples\n\n    Epoch 1/3\n\n    127656/127656 [==============================] - 1107s 9ms/step - loss: 0.0775 - acc: 0.9749 - val\\_loss: 0.0522 - val\\_acc: 0.9811\n\n    Epoch 2/3\n\n    127656/127656 [==============================] - 1044s 8ms/step - loss: 0.0528 - acc: 0.9809 - val\\_loss: 0.0522 - val\\_acc: 0.9805\n\n    Epoch 3/3\n\n    127656/127656 [==============================] - 1048s 8ms/step - loss: 0.0490 - acc: 0.9820 - val\\_loss: 0.0495 - val\\_acc: 0.9821\n\n\n\n  ### 3.  **ResNet of depth 3 in LSTM model using Glove word vector**\n\n    Train on 127656 samples, validate on 31915 samples\n\n    Epoch 1/3\n\n    127656/127656 [==============================] - 711s 6ms/step - loss: 0.0715 - acc: 0.9763 - val\\_loss: 0.0513 - val\\_acc: 0.9813\n\n    Epoch 2/3\n\n    127656/127656 [==============================] - 697s 5ms/step - loss: 0.0508 - acc: 0.9814 - val\\_loss: 0.0496 - val\\_acc: 0.9818\n\n    Epoch 3/3\n\n    127656/127656 [==============================] - 695s 5ms/step - loss: 0.0468 - acc: 0.9824 - val\\_loss: 0.0491 - val\\_acc: 0.9819\n\n\n\n  ### 4. **Bidirectional LSTM model using Glove word vector**\n\n    Train on 143613 samples, validate on 15958 samples\n\n    Epoch 1/2\n\n    143613/143613 [==============================] - 833s 6ms/step - loss: 0.0582 - acc: 0.9798 - val\\_loss: 0.0488 - val\\_acc: 0.9823\n\n    Epoch 2/2\n\n    143613/143613 [==============================] - 818s 6ms/step - loss: 0.0441 - acc: 0.9834 - val\\_loss: 0.0455 - val\\_acc: 0.9833\n\n\n\n  ### 5.  **3 layer deeper Bidirectional LSTM model using Glove word vector**\n\n    Train on 111699 samples, validate on 47872 samples\n\n    Epoch 1/2\n\n    111699/111699 [==============================] - 4283s 38ms/step - loss: 0.0723 - acc: 0.9760 - val\\_loss: 0.0581 - val\\_acc: 0.9793\n\n    Epoch 2/2\n\n    111699/111699 [==============================] - 4280s 38ms/step - loss: 0.0508 - acc: 0.9816 - val\\_loss: 0.0506 - val\\_acc: 0.9818\n\n\n\n  ### 6.  **ResNet of depth 3 in Bidirectional LSTM model using Glove word vector**\n\n    Train on 127656 samples, validate on 31915 samples\n\n    Epoch 1/3\n\n    127656/127656 [==============================] - 1615s 13ms/step - loss: 0.0681 - acc: 0.9771 - val\\_loss: 0.0503 - val\\_acc: 0.9817\n\n    Epoch 2/3\n\n    127656/127656 [==============================] - 1449s 11ms/step - loss: 0.0493 - acc: 0.9819 - val\\_loss: 0.0488 - val\\_acc: 0.9823\n\n    Epoch 3/3\n\n    127656/127656 [==============================] - 1315s 10ms/step - loss: 0.0452 - acc: 0.9829 - val\\_loss: 0.0468 - val\\_acc: 0.9827\n\n\n\n  ### 7.  **Bidirectional LSTM model with character encodings**\n\n    Train on 111699 samples, validate on 47872 samples\n\n    Epoch 1/3\n\n    111699/111699 [==============================] - 425s 4ms/step - loss: 0.0667 - acc: 0.9777 - val\\_loss: 0.0516 - val\\_acc: 0.9816\n\n    Epoch 2/3\n\n    111699/111699 [==============================] - 442s 4ms/step - loss: 0.0464 - acc: 0.9829 - val\\_loss: 0.0517 - val\\_acc: 0.9818\n\n    Epoch 3/3\n\n    111699/111699 [==============================] - 433s 4ms/step - loss: 0.0419 - acc: 0.9841 - val\\_loss: 0.0510 - val\\_acc: 0.9826\n\n\n\n  ### 8.  **Bidirectional LSTM model using Glove word vector and character encodings**\n\nTrain on 111699 samples, validate on 47872 samples\n\nEpoch 1/2\n\n111699/111699 [==============================] - 571s 5ms/step - loss: 0.0603 - acc: 0.9790 - val\\_loss: 0.0489 - val\\_acc: 0.9822\n\nEpoch 2/2\n\n111699/111699 [==============================] - 543s 5ms/step - loss: 0.0435 - acc: 0.9834 - val\\_loss: 0.0456 - val\\_acc: 0.9829\n\n\n \n## Discussion\n\nHere are a few ideas to keep in mind when manually optimizing hyperparameters for RNNs:\n\n- Watch out for _overfitting_, which happens when a neural network essentially \u0026quot;memorizes\u0026quot; the training data. Overfitting means you get great performance on training data, but the network\u0026#39;s model is useless for out-of-sample prediction.\n- Regularization helps: regularization methods include l1, l2, and dropout among others.\n- So have a separate test set on which the network doesn\u0026#39;t train.\n- The larger the network, the more powerful, but it\u0026#39;s also easier to overfit. Don\u0026#39;t want to try to learn a million parameters from 10,000 examples – parameters \\\u0026gt; examples = trouble.\n- More data is almost always better, because it helps fight overfitting.\n- Train over multiple epochs (complete passes through the dataset).\n- Evaluate test set performance at each epoch to know when to stop (early stopping).\n- In general, stacking layers can help.\n- For LSTMs, use the softsign (not softmax) activation function over tanh (it\u0026#39;s faster and less prone to saturation (~0 gradients)).\n- Updaters: RMSProp, AdaGrad or momentum (Nesterovs) are usually good choices. AdaGrad also decays the learning rate, which can help sometimes.\n- Finally, remember data normalization, MSE loss function + identity activation function for regression.\n\n| **Model** | **Number of Epocs** | **Accuracy** |\n| --- | --- | --- |\n| LSTM model using Glove word vector | 3 | 98.27 |\n| 3 layer deeper LSTM model using Glove word vector | 3 | 98.21 |\n| ResNet of depth 3 in LSTM model using Glove word vector | 3 | 98.19 |\n| Bidirectional LSTM model using Glove word vector | 2 | 98.33 |\n| 3 layer deeper Bidirectional LSTM model using Glove word vector | 2 | 98.18 |\n| ResNet of depth 3 in Bidirectional LSTM model using Glove word vector | 3 | 98.27 |\n| Bidirectional LSTM model with character encodings | 3 | 98.26 |\n| Bidirectional LSTM model using Glove word vector and character encodings | 2 | 98.29 |\n\n- ooHere when comparing all the models we observe that to gain better accuracy we don\u0026#39;t need to build a model with greater depth instead a shallow network with Bi-LSTM works better.\n- ooHere we also observe that adding character sequence can improve the accuracy.\n- ooTo further improve over the accuracy, we needed to clean the data, as many of the data consisted of visual representation of vulgarity.\n\n## Conclusion\n\nIn this work we presented multiple approaches for toxic comment classification. We showed that the approaches make different errors and can be combined into an ensemble with improved accuracy measure. The ensemble especially outperforms when there is high variance within the data and on classes with few examples. Some combinations such as shallow learners with deep neural networks are especially effective. Our error analysis on results of the ensemble identified difficult subtasks of toxic comment classification. We find that a large source of errors is the lack of consistent quality of labels. Additionally most of the unsolved challenges occur due to missing training data with highly idiosyncratic or rare vocabulary. Finally, we observed the use of proper character sequence vector and word vectors combination do help over the use of shallow networks.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprashuk%2Fnlp-toxic-comment-classification","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprashuk%2Fnlp-toxic-comment-classification","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprashuk%2Fnlp-toxic-comment-classification/lists"}