{"id":31642251,"url":"https://github.com/ashly1991/rnn-text-classification-keras-tf2","last_synced_at":"2026-04-20T19:06:23.985Z","repository":{"id":316242567,"uuid":"1062613884","full_name":"Ashly1991/rnn-text-classification-keras-tf2","owner":"Ashly1991","description":"IMDB sentiment analysis with Keras RNNs (LSTM/GRU). Within-batch padding, bucketing, embeddings, and masking for efficient, accurate training.","archived":false,"fork":false,"pushed_at":"2025-09-23T13:48:09.000Z","size":0,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-23T14:42:42.593Z","etag":null,"topics":["embeddings","gru","imdb","jupyter-notebook","keras","lstm","masking","rnn","sentiment-analysis","tensorflow","text-classification","tf-data"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ashly1991.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-09-23T13:44:03.000Z","updated_at":"2025-09-23T13:48:13.000Z","dependencies_parsed_at":"2025-09-23T14:42:44.201Z","dependency_job_id":null,"html_url":"https://github.com/Ashly1991/rnn-text-classification-keras-tf2","commit_stats":null,"previous_names":["ashly1991/rnn-text-classification-keras-tf2"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/Ashly1991/rnn-text-classification-keras-tf2","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ashly1991%2Frnn-text-classification-keras-tf2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ashly1991%2Frnn-text-classification-keras-tf2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ashly1991%2Frnn-text-classification-keras-tf2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ashly1991%2Frnn-text-classification-keras-tf2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ashly1991","download_url":"https://codeload.github.com/Ashly1991/rnn-text-classification-keras-tf2/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ashly1991%2Frnn-text-classification-keras-tf2/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278717449,"owners_count":26033542,"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","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["embeddings","gru","imdb","jupyter-notebook","keras","lstm","masking","rnn","sentiment-analysis","tensorflow","text-classification","tf-data"],"created_at":"2025-10-07T03:57:44.368Z","updated_at":"2025-10-07T03:57:48.528Z","avatar_url":"https://github.com/Ashly1991.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RNN Text Classification with Keras — Embeddings, Masking, and Efficient Batching\n\nThis project continues the IMDB **sentiment analysis** task and addresses key efficiency and modeling issues by:\n- using **within‑batch padding** (shorter sequences padded only to the longest in the **batch**, not the dataset),\n- introducing **word embeddings** to replace one‑hot vectors,\n- **skipping computation on padded steps** via Keras **masking**,\n- leveraging **Keras RNNs** (LSTMs/GRUs, stacked or bidirectional) to simplify code and improve performance.\n\n\u003e **Previous part (low‑level RNN, Part 1):** https://github.com/Ashly1991/rnn-text-classification-tf2  \n\n\n## What’s new in this repo (beyond Part 1)\n- **Efficient batching:** `from_generator` + **`padded_batch`** (pad to each batch’s max length).  \n- **Optional bucketing:** group sequences by similar length to reduce padding waste (helps more with larger truncation limits such as 500).  \n- **Embeddings:** compact, learnable representations replace one‑hot vectors; faster + fewer parameters for suitable `emb_dim`.  \n- **Keras RNNs:** use optimized **`LSTM`/`GRU`**, easily **stack** layers and add **Bidirectional** context.  \n- **Masking:** `Embedding(mask_zero=True)` propagates masks so RNNs skip padded steps → better, faster learning in terms of steps.  \n- **Cleaner training loop:** `model.fit` with built‑in metrics/callbacks (still compatible with custom loops if needed).\n\n## Quick model sketch\n```python\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\n\nvocab_size = 20000\nemb_dim = 128\n\nmodel = models.Sequential([\n    layers.Embedding(vocab_size, emb_dim, mask_zero=True),\n    layers.Bidirectional(layers.LSTM(128, return_sequences=False)),\n    layers.Dense(1, activation=\"sigmoid\")\n])\nmodel.compile(optimizer=\"adam\", loss=\"binary_crossentropy\", metrics=[\"accuracy\"])\n```\n\n## Training efficiency\n- **Within‑batch padding:** Build a `tf.data` pipeline from a Python generator and apply `padded_batch` so each batch pads only to its own max length.  \n- **Bucketing:** Optional length‑based grouping to avoid “one long sequence slows the whole batch”.  \n- **RaggedTensors:** Supported by many ops and Keras layers but not by `padded_batch`; ragged pipelines can be slower in practice.\n\n## How to Run\n```bash\npython -m venv .venv \u0026\u0026 source .venv/bin/activate     # Windows: .venv\\Scripts\\activate\npip install -r requirements.txt\njupyter lab rnn-text-classification-keras.ipynb\n```\n\n## Notes\n- Consider truncation (e.g., 200 or 500) and vocabulary limits for speed/quality trade‑offs.\n- Try LSTM vs GRU, stacked vs single layer, and bidirectional variants.\n- Monitor per‑batch time to see gains from bucketing.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashly1991%2Frnn-text-classification-keras-tf2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fashly1991%2Frnn-text-classification-keras-tf2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashly1991%2Frnn-text-classification-keras-tf2/lists"}