Experiments with fuzzy layers and neural networks
Project description
pytorch-fuzzy
Experiments with fuzzy layers and neural nerworks
Goals
- Get more fine-grained features from autoencoders
- Semi-supervised learning
- Anomaly detections
Installation
Package requirements:
torch>=1.8
Installation via pip:
pip install torchfuzzy
Fuzzy layer
Membership function for layer FuzzyLayer have form $\mu(x, A) = e^{ -|| [A . ~x]_{1 \cdots m} ||^2}$ where $m$ is task dimension, $A$ is transformation matrix in form
A_{(m+1) \times (m+1)} =
\left[ {\begin{array}{cccc}
s_{1} & a_{12} & \cdots & a_{1m} & c_{1}\\
a_{21} & s_{2} & \cdots & a_{2m} & c_{2}\\
\vdots & \vdots & \ddots & \vdots & c_{3}\\
a_{m1} & a_{m2} & \cdots & s_{m} & c_{m}\\
0 & 0 & \cdots & 0 & 1\\
\end{array} } \right]
with $c_{1\cdots m}$ - centroid, $s_{1\cdots m}$ - scaling factor, $a_{1\cdots m, 1\cdots m}$ - alignment coefficients and $x$ is an extended with $1$ vector $x = [x_1, x_2, \cdots, x_m, 1]$.
FuzzyLayer stores and tunes set of matricies $A^{n}, n = 1 \dots N$ where $N$ is layer's output dimension.
How it works
Let's demonstrate how FuzzyLayer works on simple 2D case generating dataset with four centroids.
This dataset consists of 2D point coordinates and centroid belongingness as label.
To each coordinate scaled noise component is added.
Resulting clustered structures are shown on picture below.
After training procedure completed (full code see here) and correct points labeling is achieved uniform distribution classification performed. On picture below yellow points are not passed through threshold of any centroid belonginess.
On this primitive example we can see that FuzzyLayer is able to learn clustered structure of underlying manifold.
In such a way FuzzyLayer can be used as anomaly detection algorithm if we interpret yellow points as outliers.
But more interesting application of FuzzyLayer is clusterization of another model outputs to get more fine-grained results.
Usage
Basic
from torchfuzzy import FuzzyLayer
x = torch.rand((10,2))
fuzzy_layer = FuzzyLayer.from_dimensions(2, 4)
inference = fuzzy_layer.forward(x)
Mamdani-like inference
Full example see here.
Mamdani fuzzy model can be represented as a ruleset:
\begin{array}{lcll}
\text{Rule}_{i-1} & : &\mathbf{IF}\ x\; is\; A_{i-1}\ &\mathbf{THEN}\ y_{i-1},\\
\text{Rule}_{i} & : &\mathbf{IF}\ x\; is\; A_{i }\ &\mathbf{THEN}\ y_{i},\\
\text{Rule}_{i+1} & : &\mathbf{IF}\ x\; is\; A_{i+1}\ &\mathbf{THEN}\ y_{i+1},\\
\end{array}
where $y_{i}$ is an scalar. Mamdani inference is denoted as:
Output = \frac{\sum \mu(x, A_{i})*y_{i}}{\sum \mu(x, A_{i})}
Straightforward implementation with FuzzyLayer:
mamdani_fis = nn.Sequential(
FuzzyLayer.from_dimensions(input_dimention, fuzzy_rules_count, trainable=True),
nn.Softmax(1),
nn.Linear(fuzzy_rules_count, output_dimention, bias=False)
)
A more correct implementation is implemented in the DefuzzyLinearLayer, the network structure takes the following form
mamdani_fis = nn.Sequential(
FuzzyLayer.from_dimensions(input_dimention, fuzzy_rules_count, trainable=True),
DefuzzyLinearLayer.from_dimensions(fuzzy_rules_count, output_dimention)
)
Publications
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 torchfuzzy-0.0.2.tar.gz.
File metadata
- Download URL: torchfuzzy-0.0.2.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ac59b7b74aaa64a29c6a1a722a059d6ae9b375d2910bdc16b4b70a1169847ab
|
|
| MD5 |
e4ac9be28991be4ae1f769cc141a177f
|
|
| BLAKE2b-256 |
9731d5a9d70a8b34e9be179599caa3f245e3c299d88c8483e7373686b0a90ca5
|
File details
Details for the file torchfuzzy-0.0.2-py3-none-any.whl.
File metadata
- Download URL: torchfuzzy-0.0.2-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
909aee973ddac2927e150ecb592ad471e823e3b174ab41cc985237ae07f3cb9d
|
|
| MD5 |
48f3ee944bcfb96f0098437c1b1bd200
|
|
| BLAKE2b-256 |
af185f439d323914a5f14102eb6250b54beb9b5689807c38991db575740d2f4a
|