A geometric approach to binary shape classification with custom activation functions and polygon boundary approximation.
This project demonstrates how to approximate convex polygons using neural networks where:
- ReLU neurons represent half-plane constraints
- Multiplication implements logical AND operations
- Geometric weight initialization speeds up convergence
- IoU (Intersection over Union) provides accurate shape-specific performance metrics
- ๐ง Custom Activation Functions: Implement AND logic through multiplication operations
- ๐ Geometric Interpretability: Each neuron represents a mathematically meaningful half-plane
- ๐จ Interactive Visualizations: See decision boundaries, training progress, and model weights
- ๐ Comprehensive Analysis: IoU-based evaluation with performance comparisons
- ๐ Multiple Shape Support: Circles, ellipses, triangles, polygons, and complex shapes
Circle | Ellipse | Triangle |
---|---|---|
![]() |
![]() |
![]() |
House Shape | Diamond | Pentagon |
---|---|---|
![]() |
![]() |
![]() |
L-Shape | S-Shape |
---|---|
![]() |
![]() |
Note: The multiply-all approach works best with convex shapes. Non-convex shapes demonstrate the mathematical limitations of intersecting half-planes.
- Python 3.8+
- TensorFlow 2.x
- NumPy, Matplotlib, Scikit-learn, SciPy
- Clone the repository:
git clone https://github.com/Dani-Luk/kaggle-wrapping-convex-shape-classifier.git
cd kaggle-wrapping-convex-shape-classifier
- Install dependencies:
pip install -r requirements.txt
- Launch Jupyter notebook:
Option A: Using python -m (Recommended for Windows)
python -m notebook NN-wrapping-shape-classifier.ipynb
Option B: Using jupyter command (if available)
jupyter notebook NN-wrapping-shape-classifier.ipynb
Option C: Using VS Code (Alternative)
- Open
NN-wrapping-shape-classifier.ipynb
directly in VS Code - VS Code will automatically detect and run the notebook
# Run a complete shape classification experiment
results = run_model(
shape_type='circle', # 'circle', 'triangle', 'polygon', etc.
n_sides=8, # Number of polygon sides (hyperplanes)
random_seed=42, # For reproducibility
radius_x=1.0, radius_y=1.0, # Shape parameters
corners=None # List of vertices in counter-clockwise order
)
print(f"Validation IoU: {results['iou']:.4f}")
The model approximates convex polygons using the mathematical principle:
Where:
- Each ReLU neuron represents a half-plane constraint:
$\text{ReLU}(w_1 x + w_2 y + b) = \max(0, w_1 x + w_2 y + b)$ - Multiplication implements AND logic: All constraints must be satisfied simultaneously
- The intersection of half-planes forms a convex polygon
- Geometric Interpretability: Each weight has clear geometric meaning
- Convex Guarantee: Mathematical properties ensure convex approximations
- Efficient Learning: Geometric initialization provides good starting points
- Visual Understanding: Decision boundaries are directly interpretable
Input(2D) โ Dense(n_sides, ReLU) โ Custom_Activation โ Dense(1, Sigmoid) โ Output
multiply_and_activation
: Direct multiplication with custom gradientslog_multiply_and_activation
: Log-space arithmetic for numerical stability
- Phase 1: Train output layer only (frozen polygon layer)
- Phase 2: Train all layers with geometric initialization
- Evaluation: IoU-based performance measurement (Intersection over Union)
Shape Type | Avg IoU | Performance |
---|---|---|
Convex Shapes | >0.95 | Excellent |
Simple Polygons | >0.80 | Good |
Non-Convex Shapes | ~0.5 | Limited* |
*Limited performance on non-convex shapes is expected due to mathematical constraints
- Circles & Ellipses: Perfect for demonstrating polygon approximation
- Triangles: Equilateral, custom, and irregular triangles
- Regular Polygons: Pentagon, hexagon, etc.
- Custom Polygons: House shapes, diamonds, stars
- Non-Convex: L-shapes, S-shapes (demonstrates limitations)
- n_sides: Number of polygon sides (typically 6-12)
- random_seed: For reproducible experiments
- n_samples: Training data size (default: 3000)
- activation_fn: Choice between multiplication variants
- Custom activation function based on a multiply-all (logical AND) operation
- Circle-based weight initialization strategy
- IoU-based evaluation for shape-specific performance
- Geometry-inspired architecture for interpretability
- Extending to 3D shapes and higher-dimensional convex sets
- Dynamic neuron allocation for misclassified regions
Contributions are welcome:
- Bug reports and feature requests
- Documentation improvements
- New shape types or experiments
- Training optimizations
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by geometric deep learning principles
- TensorFlow team for custom gradient support
- Matplotlib for visualization capabilities
If you find this project useful, feedback and contributions are welcome.