doanvinhlong18/lstm-news-classifier
06
lstm-news-classifier
Bidirectional LSTM (Keras / TensorFlow) trained for news topic classification on the HuffPost News Category Dataset.
Model Details
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: sparsecategoricalcrossentropy with class-weight balancing
- GPU: NVIDIA T4 (Kaggle)
