lstm-news-classifier

Bidirectional LSTM (Keras / TensorFlow) trained for news topic classification on the HuffPost News Category Dataset.

Model Details

Item Value
Base architecture Bidirectional LSTM (2 layers)
Task Multi-class text classification
# Classes 12
Vocab size 60,000
Max token length 256
Embedding dim 128
Test Accuracy 0.6909
Test F1 (macro) 0.6191

Labels

  • 0: business_tech
  • 1: crime_weird
  • 2: entertainment
  • 3: environment
  • 4: food_travel
  • 5: health
  • 6: home_style
  • 7: lifestyle
  • 8: news_politics
  • 9: positive
  • 10: science_edu
  • 11: sports

Usage

import pickle, json, numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.sequence import pad_sequences

lstm_model = tf.keras.models.load_model('lstm_model.keras')
with open('keras_tokenizer.pkl', 'rb') as f:
    tokenizer = pickle.load(f)
with open('label_meta.json') as f:
    meta = json.load(f)

ID2LABEL   = {int(k): v for k, v in meta['id2label'].items()}
MAX_LENGTH = meta['max_length']

def predict(text):
    seq    = tokenizer.texts_to_sequences([text])
    padded = pad_sequences(seq, maxlen=MAX_LENGTH, padding='post', truncating='post')
    probs  = lstm_model.predict(padded, verbose=0).squeeze()
    idx    = int(np.argmax(probs))
    return {'label': ID2LABEL[idx], 'score': float(probs[idx])}

print(predict('AI is transforming healthcare with new diagnostic tools'))

Training

  • Optimizer: Adam, lr=1e-3 with ReduceLROnPlateau
  • Epochs: 15 (best checkpoint by val_loss, EarlyStopping patience=4)
  • Loss: sparse_categorical_crossentropy with class-weight balancing
  • GPU: NVIDIA T4 (Kaggle)
Downloads last month
8
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support