A 3D activation function for PyTorch and TensorFlow with geometric properties.
Project description
Mobius Activations & Optimizer
A Python package for geometric deep learning in PyTorch and TensorFlow, featuring a 3D activation function and a geometrically-aware optimizer.
This package provides two primary components for deep learning models that need to handle geometric or rotational data properties. The core idea is to apply transformations inspired by the non-orientable topology of a Mobius strip to high-dimensional feature vectors.
Note: This is an experimental package created for research and educational purposes. The concepts are unconventional and should be considered proofs of concept. Use in production environments is not recommended without extensive testing.
Components
MobiusActivation: Atorch.nn.Moduleandtf.keras.layers.Layerthat applies a learnable, rotational transformation to 3D vector spaces.MobiusOptimizer: Atorch.optim.Optimizerandtf.keras.optimizers.Optimizerthat navigates the loss landscape using an orbital component in addition to standard gradient descent.
Installation
The package can be installed with pip. Optional extras are available for the required deep learning framework and visualization dependencies.
# For PyTorch + Visualization tools
pip install mobius-activations[torch,viz]
# For TensorFlow + Visualization tools
pip install mobius-activations[tensorflow,viz]
1. The MobiusActivation Layer
The MobiusActivation layer contains one or more internal mobius_blocks. Each block applies a "Mobius twist"—a magnitude-preserving rotation—to a 3D feature vector.
Mathematical Formulation
ReMU: A Single, Pure Transformation
For a single ReMU block with input vector z, the transformation a = F(z) is calculated as follows:
-
The rotation angle
θis determined by the input's magnitude||z||and a learnable parameterk(twist tightness):θ = k * ||z|| -
The input vector
zis rotated byθaround a given axis. For a Z-axis rotation, the transformation isa = R_z(θ)z, whereR_z(θ)is the rotation matrix:[ cos(θ) -sin(θ) 0 ] R_z(θ) = [ sin(θ) cos(θ) 0 ] [ 0 0 1 ]
S-ReMU: An Interference of Multiple Transformations
An S-ReMU calculates the final activation a_final as the weighted sum of multiple independent ReMU transformations (a_i), each with its own axis, twist tightness k_i, and learnable weight w_i:
a_final = Σ (w_i * a_i)
Operational Modes & Configuration
projectionMode (Default): The layer contains a singlemobius_blockand requires an input of exactly 3 features.groupedMode (Recommended): The layer contains multiplemobius_blocksand operates on high-dimensional inputs. It automatically processes features in parallel groups of 3. Thein_featuresmust be divisible by 3.- Learnable Mode (Recommended): Set
learnable=True. The layer will create and train its ownkandwparameters.
Example (PyTorch, Learnable & Grouped S-ReMU)
import torch.nn as nn
from mobius_activations.torch import MobiusActivation
# Input has 12 features. This creates a layer with 4 parallel,
# learnable S-ReMU blocks (each with 3 realities for x, y, z axes).
model = nn.Sequential(
nn.Linear(50, 12),
nn.BatchNorm1d(12),
MobiusActivation(in_features=12, mode='grouped', learnable=True, axes=['x', 'y', 'z']),
nn.Linear(12, 1)
)
Advanced Usage: Accessing Internal Parameters
For testing, visualization, or advanced analysis, you may need to access the learnable parameters (k_params, w_params) of a MobiusActivation layer. These parameters are encapsulated within the internal mobius_blocks.
Access is a multi-step process:
- Get the main
MobiusActivationlayer from your model. - Access its
.mobius_blocksattribute, which is a list (ModuleListin PyTorch). - Select the specific internal block you want to inspect by its index (e.g.,
[0]). - Access the parameters (
.k_params,.w_params) from that specific block.
# PyTorch Example: Accessing parameters from a trained grouped layer
# Assume 'model' is the trained model from the example above
# 1. Get the main layer
mobius_layer = model[2]
# 2. Access the list of blocks
internal_blocks = mobius_layer.mobius_blocks
print(f"The layer has {len(internal_blocks)} internal blocks.")
# 3. Select the first internal block
first_block = internal_blocks[0]
# 4. Access the learnable parameters of that block
k_params_of_first_block = first_block.k_params
w_params_of_first_block = first_block.w_params
print(f"First k parameter of the first block: {k_params_of_first_block[0].item()}")
2. The MobiusOptimizer
This optimizer adds an orbital component to the standard momentum-based update, causing the optimization path to spiral. This is effective at escaping saddle points and finding wider minima.
Mathematical Formulation
A standard Momentum optimizer updates weights W at step t using the gradient g_t and momentum m_t:
m_t = β * m_{t-1} + (1-β) * g_tW_{t+1} = W_t - η * m_t(whereηis the learning rate)
The MobiusOptimizer modifies this update rule:
- Descent Step:
ΔW_descent = -η * m_t - Orbital Step:
ΔW_orbital = -τ * (g_t ⟂ m_t)τ(tau) is the newtwist_ratehyperparameter.- The
⟂operator finds a vector perpendicular to the gradient and momentum. In the optimizer's 3D groups, this is the cross-product.
- Final Update:
W_{t+1} = W_t + ΔW_descent + ΔW_orbital
PyTorch Usage
from mobius_activations.torch import MobiusOptimizer
# model = YourPytorchModel()
optimizer = MobiusOptimizer(model.parameters(), lr=1e-3, twist_rate=0.1)
TensorFlow / Keras Usage
from mobius_activations.tensorflow import MobiusOptimizer
# model = YourKerasModel()
optimizer = MobiusOptimizer(learning_rate=1e-3, twist_rate=0.1)
# model.compile(optimizer=optimizer, loss='binary_crossentropy')
3. The Visualization Suite
The package includes utilities to help interpret the geometric behavior of the components.
visualize_optimizer_path: Shows the unique spiraling descent of theMobiusOptimizer.visualize_transformation_flow: Shows how aMobiusActivationlayer warps the entire feature space.visualize_neuron_state: Provides analysis of the transformation at a single point.
from mobius_activations.visualize import visualize_optimizer_path
# Example of visualizing the optimizer's path
visualize_optimizer_path(start_point=[-4, 3.5], learning_rate=0.1, twist_rate=0.15)
4. Potential Applications
These components are best suited for problems with underlying geometric or periodic properties.
- Computer Vision: Learning rotational invariance for object recognition.
- Signal Processing: Modeling phase shifts and harmonic interference.
- Natural Language Processing: Modeling semantic relationships as rotations in embedding space.
- Physics & Robotics: Modeling systems with real-world rotational dynamics.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mobius_activations-0.4.0.tar.gz.
File metadata
- Download URL: mobius_activations-0.4.0.tar.gz
- Upload date:
- Size: 20.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23d7c90d6e20c33f22ba43371749b5b67c6e5aacde604ae3a57922ff584bf4b5
|
|
| MD5 |
e8cc07a1ddbee2d11305addacc705e75
|
|
| BLAKE2b-256 |
8b543321f4ee3112c12f28e075ca88873d903f192e9b18a783aa3f7fe7c3e228
|
File details
Details for the file mobius_activations-0.4.0-py3-none-any.whl.
File metadata
- Download URL: mobius_activations-0.4.0-py3-none-any.whl
- Upload date:
- Size: 16.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9737124aff2ac03ea4189ff6fca6b52ca74ccd3da45451e90034c910c9cbabb3
|
|
| MD5 |
5998df8e88790085cbcb4900197ff171
|
|
| BLAKE2b-256 |
6cfde6110f33aaa4c9eb3d9c21a5d62f374c1637d7d31d9ee9ffdb171a88691c
|