{"id":29061688,"url":"https://github.com/mslawsky/binary-sarcasm-classifier","last_synced_at":"2026-04-24T12:05:22.917Z","repository":{"id":294329835,"uuid":"986626750","full_name":"mslawsky/binary-sarcasm-classifier","owner":"mslawsky","description":"This repository contains a binary sarcasm classifier for news headlines using TensorFlow and Keras.","archived":false,"fork":false,"pushed_at":"2025-05-19T22:45:44.000Z","size":20111,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-19T23:33:27.469Z","etag":null,"topics":["binary-text-classification","keras","natural-language-processing","news-headlines-analysis","sarcasm-detection","tensorflow","word-embeddings"],"latest_commit_sha":null,"homepage":"","language":null,"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/mslawsky.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-19T22:31:51.000Z","updated_at":"2025-05-19T22:45:47.000Z","dependencies_parsed_at":"2025-05-19T23:48:11.775Z","dependency_job_id":null,"html_url":"https://github.com/mslawsky/binary-sarcasm-classifier","commit_stats":null,"previous_names":["mslawsky/binary-sarcasm-classifier"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mslawsky/binary-sarcasm-classifier","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mslawsky%2Fbinary-sarcasm-classifier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mslawsky%2Fbinary-sarcasm-classifier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mslawsky%2Fbinary-sarcasm-classifier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mslawsky%2Fbinary-sarcasm-classifier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mslawsky","download_url":"https://codeload.github.com/mslawsky/binary-sarcasm-classifier/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mslawsky%2Fbinary-sarcasm-classifier/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262219799,"owners_count":23276888,"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":["binary-text-classification","keras","natural-language-processing","news-headlines-analysis","sarcasm-detection","tensorflow","word-embeddings"],"created_at":"2025-06-27T08:03:59.206Z","updated_at":"2026-04-24T12:05:22.804Z","avatar_url":"https://github.com/mslawsky.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sarcasm Detection in News Headlines 📰\n\n[![TensorFlow](https://img.shields.io/badge/TensorFlow-2.x-orange.svg)](https://www.tensorflow.org/)\n[![Keras](https://img.shields.io/badge/Keras-2.x-blue.svg)](https://keras.io/)\n[![Python](https://img.shields.io/badge/Python-3.6+-green.svg)](https://www.python.org/)\n[![NumPy](https://img.shields.io/badge/NumPy-1.19+-blue.svg)](https://numpy.org/)\n[![Matplotlib](https://img.shields.io/badge/Matplotlib-3.x-blue.svg)](https://matplotlib.org/)\n[![Jupyter](https://img.shields.io/badge/Jupyter-Notebook-blue.svg)](https://jupyter.org/)\n[![TensorFlow Embedding Projector](https://img.shields.io/badge/Embedding%20Projector-TensorBoard-blueviolet.svg)](https://projector.tensorflow.org/)\n\n## Overview 📖\n\nThis project demonstrates a **binary sarcasm classifier** for news headlines using [TensorFlow](https://www.tensorflow.org/) and [Keras](https://keras.io/). The repository contains three different model implementations:\n\n1. **Basic Embedding with Global Average Pooling** - A simple and efficient model for baseline performance\n2. **Bidirectional LSTM** - An advanced model architecture that captures sequence context in both directions\n3. **1D Convolutional Neural Network** - A model that extracts local patterns and features from text\n\nAll models process raw text headlines, convert them into numerical sequences using text vectorization, and predict whether a headline is **sarcastic** 😏 or **not sarcastic** 📰.\n\n---\n\n## Table of Contents 📑\n\n- [Overview](#overview-)\n- [Features](#features-)\n- [Dataset](#dataset-)\n- [Model Architectures](#model-architectures-)\n- [Getting Started](#getting-started-)\n- [Code Structure](#-code-structure)\n- [Results and Performance Comparison](#results-and-performance-comparison-)\n- [TF.Data Pipeline Optimization](#tfdata-pipeline-optimization-)\n- [Key Insights](#key-insights-)\n- [TensorFlow Embedding Projector](#tensorflow-embedding-projector-)\n- [Future Work](#future-work-)\n- [Acknowledgements](#acknowledgements-)\n- [Contact](#contact-)\n\n---\n\n## Dataset 📦\n\n- **Source:** [News Headlines Dataset for Sarcasm Detection](https://www.kaggle.com/datasets/rmisra/news-headlines-dataset-for-sarcasm-detection)\n- **Description:** News headlines labeled for sarcasm detection\n- **Format:** JSON file with headlines and binary labels (`0 = not sarcastic`, `1 = sarcastic`)\n- **Training Split:** 20,000 samples for training, remainder for validation\n- **Preprocessing:** Text standardization, tokenization, and padding to fixed length\n\n---\n\n## Model Architectures 🏗️\n\n### Basic Model with Global Average Pooling\n\n```\nInput Layer (32 tokens max)\n    ↓\nTextVectorization (10,000 vocab)\n    ↓\nEmbedding Layer (16 dimensions)\n    ↓\nGlobalAveragePooling1D\n    ↓\nDense Layer (24 units, ReLU)\n    ↓\nDense Layer (1 unit, Sigmoid)\n    ↓\nBinary Classification Output\n```\n\n### Bidirectional LSTM Model\n\n```\nInput Layer (32 tokens max)\n    ↓\nTextVectorization (10,000 vocab)\n    ↓\nEmbedding Layer (16 dimensions)\n    ↓\nBidirectional LSTM (32 units)\n    ↓\nDense Layer (24 units, ReLU)\n    ↓\nDense Layer (1 unit, Sigmoid)\n    ↓\nBinary Classification Output\n```\n\n### 1D Convolutional Neural Network\n\n```\nInput Layer (32 tokens max)\n    ↓\nTextVectorization (10,000 vocab)\n    ↓\nEmbedding Layer (16 dimensions)\n    ↓\nConv1D (128 filters, kernel size 5, ReLU)\n    ↓\nGlobalMaxPooling1D\n    ↓\nDense Layer (6 units, ReLU)\n    ↓\nDense Layer (1 unit, Sigmoid)\n    ↓\nBinary Classification Output\n```\n\n**Key Parameters:**\n- Vocabulary Size: 10,000 tokens\n- Max Sequence Length: 32 tokens\n- Embedding Dimensions: 16\n- LSTM Units: 32 (bidirectional, resulting in 64-dimensional output)\n- Conv1D Filters: 128 with kernel size 5\n- Training Examples: 20,000\n- Padding Type: 'pre'\n- Truncation Type: 'post'\n\n---\n\n## Getting Started 🛠️\n\n### Prerequisites\n\n- Python 3.x\n- TensorFlow 2.x\n- NumPy\n- Matplotlib\n- [Jupyter Notebook](https://jupyter.org/)\n\n### Installation\n\n```bash\ngit clone https://github.com/yourusername/sarcasm-detection\ncd sarcasm-detection\npip install -r requirements.txt\n```\n\n### Usage\n\n1. Download the sarcasm dataset from [Kaggle](https://www.kaggle.com/datasets/rmisra/news-headlines-dataset-for-sarcasm-detection) or use the provided `sarcasm.json` file.\n2. Open and run the desired notebook:\n   - `C3_W2_Lab_2_sarcasm_classifier.ipynb` - Basic model with GlobalAveragePooling\n   - `C3_W3_Lab_5_sarcasm_with_bi_LSTM.ipynb` - Advanced model with Bidirectional LSTM\n   - `C3_W3_Lab_6_sarcasm_with_1D_convolutional.ipynb` - Model with 1D Convolutional layer\n3. Follow the notebook steps to:\n   - Load and preprocess the data\n   - Build and compile the model\n   - Train with customizable parameters\n   - Evaluate performance and visualize results\n4. Export embedding weights for visualization in the [TensorFlow Embedding Projector](https://projector.tensorflow.org/).\n\n---\n\n## 📂 Code Structure\n\n- `C3_W2_Lab_2_sarcasm_classifier.ipynb` - Basic model implementation with GlobalAveragePooling\n- `C3_W3_Lab_5_sarcasm_with_bi_LSTM.ipynb` - Bidirectional LSTM model implementation\n- `C3_W3_Lab_6_sarcasm_with_1D_convolutional.ipynb` - 1D Convolutional model implementation\n- `sarcasm.json` - Dataset file (download separately)\n- `requirements.txt` - List of dependencies\n- `vecs.tsv` - Exported word vectors (generated after training)\n- `meta.tsv` - Exported metadata (generated after training)\n\n---\n\n## Results and Performance Comparison 📊\n\n### Basic Model (Global Average Pooling)\n- **Training Accuracy:** ~96% after 10 epochs\n- **Validation Accuracy:** ~84% (with some overfitting observed)\n- **Model Size:** Fewer parameters, computationally efficient\n- **Training Speed:** Fastest training time\n- **Advantages:** Simple architecture, good baseline performance\n\n![Training Curve](training-curve.png)\n\n### Bidirectional LSTM Model\n- **Training Accuracy:** ~97% after 10 epochs\n- **Validation Accuracy:** ~84-85% \n- **Model Size:** More parameters (174,129) but still lightweight\n- **Training Speed:** Slower than the basic model but captures sequential information\n- **Advantages:** Better captures word order and context in both directions\n\n![Training Curve](training-curve-2.png)\n\n### 1D Convolutional Model\n- **Training Accuracy:** ~98% after 10 epochs\n- **Validation Accuracy:** ~85-86%\n- **Model Size:** 139,399 parameters (less than LSTM, more than basic model)\n- **Training Speed:** Faster than LSTM but slower than the basic model\n- **Advantages:** Captures local n-gram patterns effectively, good at detecting key phrases\n\n![Training Curve](training-curve-3.png)\n\n### Training Curves\nThe notebooks generate visualizations showing:\n- Accuracy progression over epochs\n- Loss reduction during training\n- Training vs. validation performance comparison\n\n---\n\n## TF.Data Pipeline Optimization 🔄\n\nBoth models use TensorFlow's efficient data pipeline (`tf.data.Dataset`) with optimizations:\n- Data caching\n- Prefetching\n- Shuffling with buffer\n- Batching\n- Efficient sequence padding\n\nThis results in faster training times and better resource utilization.\n\n---\n\n## Key Insights 🔍\n\n1. **Bidirectional LSTM** captures word order and context in both directions, potentially improving performance on sequence-sensitive tasks\n2. **1D Convolutional layers** with GlobalMaxPooling effectively identify the most important n-gram features in the text\n3. **Global Average Pooling** model provides a simpler architecture with fewer parameters\n4. **Text Vectorization** layer provides efficient preprocessing integrated into the model\n5. All models show signs of **overfitting** after several epochs\n6. **Validation accuracy** is similar across all three models (~84-86%), suggesting that for this specific dataset, the architecture choices provide marginal improvements\n7. **Hyperparameter tuning** opportunities exist for vocabulary size, embedding dimensions, LSTM units, convolutional filters, and dense layer architecture\n\n---\n\n## TensorFlow Embedding Projector 🌐\n\nVisualize the learned word embeddings with the [TensorFlow Embedding Projector](https://projector.tensorflow.org/):\n\n1. After training, export embedding weights and metadata to `vecs.tsv` and `meta.tsv`.\n2. Upload these files to the Embedding Projector.\n3. Explore word relationships and clusters in the learned embedding space.\n4. Discover how the model represents sarcastic vs. non-sarcastic language patterns.\n\n![Demo Embedding Projector](demo.gif)\n\n---\n\n## Future Work 🌱\n\n- Experiment with different architectures (GRU, Transformer-based models)\n- Implement regularization techniques to reduce overfitting (dropout, L2 regularization)\n- Stack multiple Bidirectional LSTM layers for deeper context understanding\n- Try hybrid models combining CNN and RNN features\n- Experiment with different Conv1D filter sizes and number of filters\n- Try different vocabulary sizes and embedding dimensions\n- Add attention mechanisms for better context understanding\n- Explore transfer learning with pre-trained embeddings (Word2Vec, GloVe)\n- Multi-class classification for different types of sarcasm\n- Ensemble multiple model types for potentially better performance\n\n---\n\n## Acknowledgements 🙏\n\nSpecial thanks to:\n- [Andrew Ng](https://www.andrewng.org/) for creating the Deep Learning AI curriculum\n- [Laurence Moroney](https://twitter.com/lmoroney) for excellent instruction and developing the course materials  \n- The creators of the [News Headlines Dataset for Sarcasm Detection](https://www.kaggle.com/datasets/rmisra/news-headlines-dataset-for-sarcasm-detection)\n- This notebook was created as part of the TensorFlow Developer Certificate program by DeepLearning.AI\n\n---\n\n## Contact 📫\n\nFor inquiries about this project:\n- [LinkedIn Profile](https://www.linkedin.com/in/melissaslawsky/)\n- [Client Results](https://melissaslawsky.com/portfolio/)\n- [Tableau Portfolio](https://public.tableau.com/app/profile/melissa.slawsky1925/vizzes)\n- [Email](mailto:melissa@melissaslawsky.com)\n\n---\n\n© 2025 Melissa Slawsky. All Rights Reserved.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmslawsky%2Fbinary-sarcasm-classifier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmslawsky%2Fbinary-sarcasm-classifier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmslawsky%2Fbinary-sarcasm-classifier/lists"}