An experimental neural network architecture ported from Java that utilizes sophisticated feature extraction and "shaping" functions. This network is designed for reliable classification tasks, such as facial recognition, by leveraging cosine similarity measures.
Unlike traditional backpropagation-based networks, this architecture performs well with single-class/label training, eliminating the need for complex weight balancing to achieve accurate predictions.
- Shaping Functions: Uses mathematical "shapes" (Mean, Log-Mean, Min, Max) to adapt weights.
- Cosine Similarity: Measures the orientation of vectors rather than just magnitude, providing robustness against scaling.
- Partition Layers: Automatically selects and optimizes the best-fitting neurons for specific features.
- NumPy Backend: Optimized for numerical vector operations in Python.
Install from PyPI:
pip install shaping-networkThis will automatically install NumPy as a dependency.
📦 Package available on PyPI: https://pypi.org/project/shaping-network/
Use the ShapingNetworkBuilder to define the architecture. You can connect layers to specific target ranges.
from shaping_network import ShapingNetworkBuilder
from layers import LayerBuilder
from shapes import Shapes
model = ShapingNetworkBuilder() \
.connect_in_range(0, 1, # Adds layers for targets 0 through 1
LayerBuilder()
.add_layer(Shapes.MEAN, 2) # Shaping function and number of nodes
).build()Set the input dimension (number of features) and randomize the initial sum vectors.
# Initialize with 3 input nodes
model.initialize(3)
model.randomize()Data is fed into the model using ContextVector objects.
from ctx_vector import ContextVector
training_data = {
0: [ # Class 0
ContextVector([0.001, 2.531, 1.523]),
ContextVector([0.009, 2.231, 1.241])
],
1: [ # Class 1
ContextVector([1.8, 0.001, 4.9]),
ContextVector([2.5, 0.002, 4.5])
]
}Training is performed by "feeding" vectors to the model along with their target labels.
epochs = 100
for i in range(epochs):
for target, vectors in training_data.items():
for vec in vectors:
model.feed(vec, target) # Adjusts the weights/shapes for that targetThe predict method returns a list of probabilities (similarity scores) for each layer/class.
prediction = model.predict(test_vector)
# Get the index of the highest probability
max_index = prediction.index(max(prediction))
print(f"Predicted Class: {max_index}")The network converges quickly to high accuracy:
Accuracy: 0.998521
Class 0:
Index=0, Probability=0.999873
Class 1:
Index=1, Probability=0.997032
The core of the network is the Shaping Function. Instead of traditional gradient descent, neurons maintain a sum_vector and a shape_vector. When feed() is called:
- The
sum_vectoraccumulates the input data. - The
shaping_function(e.g.,LOG_MEAN) is applied to derive a newshape_vector. - Prediction is calculated as the Cosine Similarity between the input vector and the neuron's
shape_vector.
This project is licensed under the MIT License - see the LICENSE file for details.