How taotao917/machine-learning-design-patterns-detection works
Purpose and execution model
This is a Python 3.10+ batch ML pipeline that classifies free text into Machine Learning Design Pattern (MLDP) categories using TensorFlow/Keras TextCNN. It is operated from the repository root through module entry points: scripts/build_tokenizer.py, scripts/train.py, scripts/evaluate.py, and scripts/explain_lime.py. Central behavior is configured by configs/textcnn.yaml.
Input data and label boundary
Ready-to-run supervised splits are TSV-like, one text\tlabel_id record per line: data/train_data.txt and data/val_data.txt. Class IDs are 0-based and may be translated to display names through data/labels/id_to_name.json. Raw-data acquisition is optional and isolated in scripts/download_data.sh; training consumes prepared splits rather than requiring external collection.
Tokenization and vectorization stage
scripts/build_tokenizer.py fits and persists the text representation used by later stages. The data abstraction in utils/data_io.py loads records, encodes labels, and vectorizes text for either whitespace tokenization or a Hugging Face tokenizer mode. Tokenizer mode, vocabulary limits, token normalization, and sequence length originate in configs/textcnn.yaml; generated tokenizer/statistics artifacts form the compatibility boundary between training, evaluation, and explanation.
TextCNN model boundary
models/textcnn.py defines the serializable classifier: embedding input, parallel multi-width Conv1D feature extractors, global max pooling, concatenation, dropout, and softmax classification. It also provides ReverseTemporal, supporting a reversed temporal stream. Model width, embedding size, filter sizes, dropout, regularization, and number of classes are configuration-controlled in configs/textcnn.yaml. Consumers loading saved models must register this custom layer.
Training orchestration
scripts/train.py combines configured data/vectorization with the TextCNN model and executes Keras training. It applies reproducibility controls from utils/seed.py, training callbacks from utils/callbacks.py, and optional imbalance strategies—balanced class weights or focal loss—under config control. The run writes a timestamped model checkpoint plus resolved config and metrics under the configured checkpoints/textcnn/ root.
Evaluation and reporting
scripts/evaluate.py loads a saved checkpoint, reconstructs the matching tokenizer/vectorization path, and scores the validation split. Metrics and visual reporting are centralized in utils/metrics.py; evaluation produces classification reports and confusion-matrix/per-class PRF figures below the configured results/ root. This is an offline reporting flow, not an inference service.
Explainability flow
scripts/explain_lime.py loads the trained model and tokenizer and exposes a raw-text prediction wrapper to LIME. It can select a validation example or accept direct text, then emits token-attribution HTML artifacts under results. The explanation boundary is intentionally raw text: LIME tokenization is aligned with the whitespace tokenizer’s code-token preservation behavior, while model-facing vectorization remains delegated to utils/data_io.py.
Runtime and dependencies
The primary runtime is Python with TensorFlow/Keras for model construction, training, persistence, and inference. Supporting dependencies declared in requirements.txt include data/plotting and LIME capabilities; optional Hugging Face tokenizer support is accommodated by utils/data_io.py. The only described external service is optional Google Drive download via scripts/download_data.sh; the normal pipeline runs entirely from repository-local data and generated artifacts.
Main entry points: train.py Train model · evaluate.py Evaluate checkpoint · explain_lime.py LIME explanations · textcnn.yaml TextCNN configuration
Components 18
Data & representation
- Optional data download · Optionally downloads raw data from Google Drive.
scripts/download_data.sh - Training split · One text and numeric label per record.
data/train_data.txt - Validation split · Used for evaluation and optional explanation selection.
data/val_data.txt - Label display names · Maps zero-based class IDs to names.
data/labels/id_to_name.json - Data loading & vectorization · Loads records, encodes labels, and supports whitespace or Hugging Face tokenizers.
utils/data_io.py - Build tokenizer · Fits and persists the shared text representation.
scripts/build_tokenizer.py - Tokenizer & statistics · Compatibility boundary for training, evaluation, and explanation.
Training
- TextCNN classifier · Embedding, parallel Conv1D/max-pool features, dropout, and softmax; includes ReverseTemporal.
models/textcnn.py - Train model · Runs configured Keras training and optional imbalance handling.
scripts/train.py - Reproducibility controls
utils/seed.py - Training callbacks
utils/callbacks.py - Model checkpoints · Timestamped saved model, resolved config, and training metrics.
Offline analysis
- Evaluate checkpoint · Scores the validation split using matching vectorization.
scripts/evaluate.py - Metrics & plots · Creates reports, confusion matrices, and per-class PRF figures.
utils/metrics.py - LIME explanations · Explains raw-text predictions while delegating model vectorization.
scripts/explain_lime.py - Reports & explanations · Evaluation figures, reports, and token-attribution HTML.
Other components
- TextCNN configuration · Controls vectorization, model, imbalance, and run settings.
configs/textcnn.yaml - Python dependencies · TensorFlow/Keras, plotting, LIME, and optional Hugging Face support.
requirements.txt
Connections 28
- Optional data download → Training split: prepares
- Optional data download → Validation split: prepares
- TextCNN configuration → Build tokenizer: tokenizer settings
- Training split → Build tokenizer: fit text
- Build tokenizer → Tokenizer & statistics: persists
- TextCNN configuration → Data loading & vectorization: vectorization settings
- Data loading & vectorization → Tokenizer & statistics: loads/saves representation
- TextCNN configuration → TextCNN classifier: architecture settings
- Training split → Train model: supervised records
- Data loading & vectorization → Train model: vectorized inputs
- Tokenizer & statistics → Train model: shared representation
- TextCNN classifier → Train model: builds classifier
- Reproducibility controls → Train model: sets seeds
- Training callbacks → Train model: applies callbacks
- Train model → Model checkpoints: writes model and run metadata
- Model checkpoints → Evaluate checkpoint: loads saved model
- Validation split → Evaluate checkpoint: scores
- Tokenizer & statistics → Evaluate checkpoint: reconstructs inputs
- Evaluate checkpoint → Metrics & plots: metrics inputs
- Label display names → Metrics & plots: class names
- Metrics & plots → Reports & explanations: writes reports and figures
- Model checkpoints → LIME explanations: loads model
- Tokenizer & statistics → LIME explanations: vectorizes predictions
- Validation split → LIME explanations: optional example
- LIME explanations → Reports & explanations: writes attribution HTML
- Python dependencies → Train model: runtime
- Python dependencies → Evaluate checkpoint: runtime
- Python dependencies → LIME explanations: runtime