Instructions to use doanvinhlong18/lstm-news-classifier with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use doanvinhlong18/lstm-news-classifier with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://doanvinhlong18/lstm-news-classifier") - Notebooks
- Google Colab
- Kaggle
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_tech1: crime_weird2: entertainment3: environment4: food_travel5: health6: home_style7: lifestyle8: news_politics9: positive10: science_edu11: 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