Text preprocessing: Raw tweet text, tokenised with tf.keras.preprocessing.text.Tokenizer, padded to max_length=100
Note: Training was performed on a small subset (~1,600 samples). The full Sentiment140 dataset contains 1.6M tweets. The preprocessing pipeline in this repository (src/spark/preprocessing.py) operates on the full dataset using PySpark, but the Keras model was trained on a Colab subset for prototyping purposes.
Training Procedure
python
1model.fit(2 X_train, y_train,3 validation_split=0.2,# ~320 validation samples4 epochs=5,# training ran all 5 epochs5 batch_size=64,6 callbacks=[early_stopping, reduce_lr]7)
Training log (Google Colab run)
Epoch
Train Accuracy
Val Accuracy
Val Loss
Learning Rate
1
68.5%
100.0%
0.173
0.001
2
99.7%
100.0%
0.093
0.001
3
99.9%
100.0%
0.071
0.001
4
100.0%
100.0%
0.043
0.001
5
100.0%
100.0%
0.023
0.001
Best checkpoint saved at epoch 5 (lowest val_loss = 0.023).
Evaluation
Important caveats
These results should not be interpreted as production accuracy:
Small training set: ~1,280 training samples and ~320 validation samples. The 100% validation accuracy is consistent with a model that has memorised a small, potentially non-representative validation split.
AUC metric not reliable: The AUC metric reported 0.0 throughout all training epochs. This is a known incompatibility between tf.keras.metrics.AUC() and certain TensorFlow/Keras version combinations when used with a sigmoid output and binary cross-entropy loss without explicit threshold configuration. AUC values are therefore excluded from this card.
No held-out test set evaluation: Evaluation was not performed on a separate, never-seen test set after training.
For reliable performance estimates on binary Twitter sentiment, refer to the Spark MLlib classifiers evaluated on the full dataset (see README.md):
Model
Accuracy
F1 (weighted)
Dataset size
Random Forest
70.3%
70.3%
~233K rows
Gradient Boosted Trees
69.9%
69.8%
~233K rows
Logistic Regression
68.5%
68.5%
~233K rows
Naive Bayes
67.2%
67.3%
~233K rows
Files in this Repository
File
Description
Size
best_LSTM_pipeline_model.h5
Best checkpoint by validation loss
~17 MB
pipeline_lstm_model.h5
Final epoch weights
~17 MB
Both files are in HDF5 format (legacy Keras format). The native Keras .keras format is recommended for new training runs but these weights are fully loadable with tf.keras.models.load_model.
How to Use
Load and run inference
python
1import tensorflow as tf
2import numpy as np
34# Load model5model = tf.keras.models.load_model("best_LSTM_pipeline_model.h5")67# Reproduce the tokeniser (must match training)8from tensorflow.keras.preprocessing.text import Tokenizer
9from tensorflow.keras.preprocessing.sequence import pad_sequences
1011tokenizer = Tokenizer(num_words=10000)12# tokenizer must be fitted on the same training texts used during training13# (the tokenizer state is not saved alongside the .h5 file)1415# Preprocess new text16texts =["I love this product!","Terrible experience, would not recommend."]17sequences = tokenizer.texts_to_sequences(texts)18padded = pad_sequences(sequences, maxlen=100)1920# Predict21predictions = model.predict(padded)22labels =["positive"if p >0.5else"negative"for p in predictions.flatten()]23print(labels)
Important: The Tokenizer vocabulary is not saved in the .h5 file. To reproduce predictions, you must refit the tokenizer on the same training texts. This is a known limitation of this prototype — a future improvement would be to save the tokenizer alongside the model weights (e.g., as tokenizer.json).