← all repositories
Shawn1993/cnn-text-classification-pytorch

A 2014 paper, resurrected for PyTorch 2.0

This repo modernizes Yoon Kim's classic CNN sentence classifier without letting it drift from the original results.

1k stars Python Language ModelsML Frameworks
cnn-text-classification-pytorch
Velocity · 7d
+0.3
★ / day
Trend
steady
star history

What it does Implements Kim’s 2014 “Convolutional Neural Networks for Sentence Classification” in modern PyTorch. It trains 1-D convolutions over word embeddings for binary (MR) or fine-grained 5-class (SST) sentiment classification, with a CLI for training, testing, and one-off prediction.

The interesting bit The value is in the archaeology: the author didn’t just port the model, they stripped out deprecated torchtext APIs, fixed half a dozen RuntimeErrors and IndexErrors that accumulate when old PyTorch code meets 2.0+, and added the missing L2 constraint on the fully-connected layer that Kim’s original actually used. Results match the paper: 76.5% on MR, 45.6% on SST.

Key highlights

  • Reproduces Kim’s CNN-rand baseline within ~0.5% on both MR and SST datasets
  • Removed torchtext dependency; uses standard Dataset/DataLoader with a custom BucketSampler
  • Supports both Adam and Adadelta optimizers, plus phrase-level SST training data
  • CLI includes train/test/predict modes with snapshot loading
  • Requires only PyTorch ≥2.0 and Python ≥3.8

Caveats

  • Input text must be space-separated, even punctuation, and longer than your largest kernel size or it breaks
  • Only two datasets (MR, SST) are bundled; you’ll need to wire your own data loader for anything else
  • The 2017 snapshot path in the README predict examples is stale; you’ll need to point to your own trained model

Verdict Useful if you need a clean, reproducible baseline for sentence classification or you’re teaching the Kim paper and want code that actually runs on modern PyTorch. Skip it if you need multilingual support, transformer baselines, or a production-ready pipeline.

heatdrop uses Google Analytics to see which pages get read — nothing else. Your call. How we handle data.