AutoInt Tutorial: Automatic Feature Interaction Learning via Self-Attention#
Sandbox Model — Experimental
This model is part of the CoreRec Sandbox — an experimental collection of algorithms for research and exploration. Sandbox models are not production-tested and may have incomplete implementations. For production-ready models, see the Production Models.
Introduction#
AutoInt uses multi-head self-attention to automatically learn high-order feature interactions without manual feature engineering.
Paper: Song et al. 2019 - AutoInt: Automatic Feature Interaction Learning via Self-Attentive Neural Networks
How AutoInt Works#
Architecture#
Self-Attention for Feature Interactions:
Embedding Layer: Convert sparse features to dense embeddings
Interacting Layer: Multi-head self-attention on feature embeddings
Each head learns different interaction patterns
Residual connections preserve original features
Stacking: Multiple attention layers for higher-order interactions
Output Layer: Combine all layers for prediction
Key Innovation: Treats features as a sequence, applies self-attention
Architecture:
Features → Embed → [Self-Attention + Residual]×L → Concat → Output
Mathematical Foundation#
Multi-Head Self-Attention:
head_h = Attention(E·W_h^Q, E·W_h^K, E·W_h^V)
where E = [e_1, e_2, ..., e_m] # feature embeddings
Attention Mechanism:
Attention(Q,K,V) = softmax((Q·K^T)/√d) · V
Multi-Head Output:
MultiHead(E) = [head_1; head_2; ...; head_H] × W^O
Residual Connection:
E' = ReLU(MultiHead(E) + E)
Final Prediction:
ŷ = σ(w^T · [E^(0); E^(1); ...; E^(L)])
where E^(l) is output of layer l
Reference Implementation#
Implementation Note
The code examples below are reference implementations for learning purposes. This is a sandbox model and has not been production-tested. Verify behavior thoroughly before using in production.
Step 1: Import and Load Data#
from corerec.sandbox.collaborative_full.nn_base.AutoInt_base import AutoInt_base as AutoInt
from cr_learn import ml_1m
from sklearn.model_selection import train_test_split
import numpy as np
# Load dataset with CORRECT API
data = ml_1m.load() # Returns dict with 'ratings', 'users', 'movies'
ratings_df = data['ratings'] # DataFrame with user_id, movie_id, rating, timestamp
print(f"Loaded {len(ratings_df)} ratings")
# Split data
train_df, test_df = train_test_split(ratings_df, test_size=0.2, random_state=42)
# Extract arrays for model
train_users = train_df['user_id'].values
train_items = train_df['movie_id'].values
train_ratings = train_df['rating'].values
test_users = test_df['user_id'].values
test_items = test_df['movie_id'].values
test_ratings = test_df['rating'].values
Step 2: Initialize Model#
model = AutoInt(
name="AutoInt_Model",
embedding_dim=32,
attention_dim=32,
num_heads=2,
num_layers=3,
dropout=0.1,
epochs=20,
batch_size=256,
learning_rate=0.001,
verbose=True
)
print(f"Initialized {model.name}")
Step 3: Train#
model.fit(
user_ids=train_users,
item_ids=train_items,
ratings=train_ratings
)
print("Training complete!")
Step 4: Predict#
# Single prediction
score = model.predict(user_id=1, item_id=100)
print(f"Predicted score: {score:.3f}")
# Batch predictions
test_predictions = model.batch_predict(list(zip(test_users[:100], test_items[:100])))
Step 5: Recommend#
# Get top-10 recommendations for user
user_id = 1
recommendations = model.recommend(
user_id=user_id,
top_k=10
)
print(f"Top-10 recommendations for User {user_id}:")
for rank, item_id in enumerate(recommendations, 1):
print(f" {rank}. Item {item_id}")
Step 6: Evaluate#
from sklearn.metrics import mean_squared_error
import numpy as np
# Predict all test ratings
test_pred = [model.predict(u, i) for u, i in zip(test_users, test_items)]
rmse = np.sqrt(mean_squared_error(test_ratings, test_pred))
print(f"Test RMSE: {rmse:.4f}")
Step 7: Save & Load#
# Save model
model.save('artifacts/autoint_model')
# Load model
loaded = AutoInt.load('artifacts/autoint_model')
test_score = loaded.predict(1, 100)
print(f"Loaded model prediction: {test_score:.3f}")
Key Takeaways#
When to Use AutoInt#
✅ Excellent For:
Feature-rich CTR prediction
When manual feature engineering is expensive
Discovering unknown feature interactions
Sparse categorical features
Display advertising, app recommendations
❌ Not For:
Simple datasets (overkill)
No features available (use CF)
Sequential patterns (use RNN)
Very large feature spaces (memory intensive)
Best Practices#
Attention Heads: 2-4 heads sufficient
Layers: 2-3 interacting layers
Embedding Dim: 16-64 per feature
Attention Dim: Same as embedding
Residual: Always use residual connections
Dropout: 0.1-0.2 on attention weights
Learning Rate: 0.001 with warmup
Batch Size: 256-1024
Scaling & Production Considerations#
If you plan to move this sandbox model toward production use, consider:
Data Volume: Test with realistic dataset sizes; sandbox implementations may not be optimized for large-scale data.
Distributed Training: For datasets exceeding single-machine memory, consider wrapping the model in distributed training frameworks (e.g., PyTorch DDP, Horovod).
Hyperparameter Tuning: Use systematic search (Optuna, Ray Tune) rather than manual tuning. The defaults in sandbox models are not tuned for any specific domain.
Serving Latency: Profile inference time. Some architectures (e.g., attention-based) may need quantization or ONNX export for low-latency serving.
Monitoring: Implement A/B testing and online metrics (CTR, engagement, diversity) before replacing an existing system.
Validation: Write comprehensive unit tests and integration tests before deploying. Sandbox models do not have CI coverage.
Further Reading#
Paper: Song et al. 2019 - AutoInt: Automatic Feature Interaction Learning via Self-Attentive Neural Networks