A neural network implementation using shaping functions
Project description
Shape Network (Python Edition)
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.
Features
- 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.
Installation
This project requires NumPy.
pip install numpy
Usage
1. Build the Network
Use the ShapingNetworkBuilder to define the architecture. You can connect layers to specific target ranges.
from shape_network import ShapingNetworkBuilder, LayerBuilder, 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()
2. Initialize and Randomize
Set the input dimension (number of features) and randomize the initial sum vectors.
# Initialize with 3 input nodes
model.initialize(3)
model.randomize()
3. Prepare Training Data
Data is fed into the model using ContextVector objects.
from shape_network 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])
]
}
4. Training
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 target
5. Predict
The 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}")
Example Output
The network converges quickly to high accuracy:
Accuracy: 0.998521
Class 0:
Index=0, Probability=0.999873
Class 1:
Index=1, Probability=0.997032
Mathematical Overview
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.
License
This project is licensed under the MIT License - see the LICENSE file for details.
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 shaping_network-0.1.1.tar.gz.
File metadata
- Download URL: shaping_network-0.1.1.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c8fc67f91fb8f2d17eb6f3b88319c54a878e93bf07a84f7a802877dde2851ed
|
|
| MD5 |
6a751411a0c71c1c77ef072b4cdab3d7
|
|
| BLAKE2b-256 |
9ec5e157411315943f7dc58898b1ec9086f661c1c68b8bfe1245ac9300adf8bc
|
File details
Details for the file shaping_network-0.1.1-py3-none-any.whl.
File metadata
- Download URL: shaping_network-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ff747a19ea300798e1213c88b14cf1aa75a7422f2ec3baa723e5f0f2bb51ea2
|
|
| MD5 |
8d4cb8e8ebe3d35ccb9a92cbd0633574
|
|
| BLAKE2b-256 |
aaea5d4b03176ef52ba6fcbcf6c1bef39c5d21431b52f588b8ccb9c88252d800
|