torchfuzzy


Nametorchfuzzy JSON
Version 0.0.2 PyPI version JSON
download
home_pageNone
SummaryExperiments with fuzzy layers and neural networks
upload_time2024-10-24 14:33:05
maintainerNone
docs_urlNone
authorGurov Yury, Khilkov Danil
requires_python>=3.7
licenseNone
keywords fuzzy
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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:

```python
 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](https://en.wikipedia.org/wiki/Transformation_matrix) in form 

```math
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. 

![image](https://user-images.githubusercontent.com/6205671/211149471-9d850748-f40b-4acc-8250-331b5594ffe0.png)


After training procedure completed (full code see [here](experiments_simple_clustering.py)) and correct points labeling is achieved uniform distribution classification performed. On picture below yellow points are not passed through threshold of any centroid belonginess.

![image](https://user-images.githubusercontent.com/6205671/211149065-b72b1e11-a538-479b-813a-df4e06ab115c.png)
![image](https://user-images.githubusercontent.com/6205671/214388927-6e70dcf1-2323-4ac9-8589-144e96a6375d.png)

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

```python
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](experiments_mamdani_mnist.ipynb).

Mamdani fuzzy model can be represented as a ruleset:

```math
    \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:

```math
Output = \frac{\sum \mu(x, A_{i})*y_{i}}{\sum \mu(x, A_{i})}
```

Straightforward implementation with `FuzzyLayer`:

```python
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

```python
mamdani_fis = nn.Sequential(
            FuzzyLayer.from_dimensions(input_dimention, fuzzy_rules_count, trainable=True),
            DefuzzyLinearLayer.from_dimensions(fuzzy_rules_count, output_dimention)
        )
```

## Publications

[Variational Autoencoders with Fuzzy Inference (Russian)](https://habr.com/ru/articles/803789/)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "torchfuzzy",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "fuzzy",
    "author": "Gurov Yury, Khilkov Danil",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/97/31/d5a9d70a8b34e9be179599caa3f245e3c299d88c8483e7373686b0a90ca5/torchfuzzy-0.0.2.tar.gz",
    "platform": null,
    "description": "# pytorch-fuzzy\n\nExperiments with fuzzy layers and neural nerworks\n\n## Goals\n\n- Get more fine-grained features from autoencoders\n- Semi-supervised learning\n- Anomaly detections\n\n## Installation\n\nPackage requirements:\n\n`\ntorch>=1.8\n`\n\nInstallation via pip:\n\n```python\n pip install torchfuzzy\n```\n\n## Fuzzy layer\n\nMembership 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](https://en.wikipedia.org/wiki/Transformation_matrix) in form \n\n```math\nA_{(m+1) \\times (m+1)} =\n  \\left[ {\\begin{array}{cccc}\n    s_{1} & a_{12} & \\cdots & a_{1m} & c_{1}\\\\\n    a_{21} & s_{2} & \\cdots & a_{2m} & c_{2}\\\\\n    \\vdots & \\vdots & \\ddots & \\vdots & c_{3}\\\\\n    a_{m1} & a_{m2} & \\cdots & s_{m} & c_{m}\\\\\n    0 & 0 & \\cdots & 0 & 1\\\\\n  \\end{array} } \\right]\n\n```\n\nwith $c_{1\\cdots m}$ - centroid, \n$s_{1\\cdots m}$ - scaling factor, \n$a_{1\\cdots m, 1\\cdots m}$ - alignment coefficients and \n$x$ is an extended with $1$ vector \n$x = [x_1, x_2, \\cdots, x_m, 1]$.\n\n`FuzzyLayer` stores and tunes set of matricies $A^{n}, n = 1 \\dots N$ where $N$ is layer's output dimension.\n\n## How it works\n\nLet's demonstrate how `FuzzyLayer` works on simple 2D case generating dataset with four centroids. \nThis dataset consists of 2D point coordinates and centroid belongingness as label.\nTo each coordinate scaled noise component is added.\nResulting clustered structures are shown on picture below. \n\n![image](https://user-images.githubusercontent.com/6205671/211149471-9d850748-f40b-4acc-8250-331b5594ffe0.png)\n\n\nAfter training procedure completed (full code see [here](experiments_simple_clustering.py)) and correct points labeling is achieved uniform distribution classification performed. On picture below yellow points are not passed through threshold of any centroid belonginess.\n\n![image](https://user-images.githubusercontent.com/6205671/211149065-b72b1e11-a538-479b-813a-df4e06ab115c.png)\n![image](https://user-images.githubusercontent.com/6205671/214388927-6e70dcf1-2323-4ac9-8589-144e96a6375d.png)\n\nOn this primitive example we can see that `FuzzyLayer` is able to learn clustered structure of underlying manifold.\nIn such a way `FuzzyLayer` can be used as anomaly detection algorithm if we interpret yellow points as outliers. \nBut more interesting application of `FuzzyLayer` is clusterization of another model outputs to get more fine-grained results.\n\n## Usage\n\n### Basic\n\n```python\nfrom torchfuzzy import FuzzyLayer\n\nx = torch.rand((10,2))\n\nfuzzy_layer = FuzzyLayer.from_dimensions(2, 4)\n\ninference = fuzzy_layer.forward(x)\n\n```\n\n### Mamdani-like inference\n\nFull example [see here](experiments_mamdani_mnist.ipynb).\n\nMamdani fuzzy model can be represented as a ruleset:\n\n```math\n    \\begin{array}{lcll}\n        \\text{Rule}_{i-1} & : &\\mathbf{IF}\\ x\\; is\\; A_{i-1}\\ &\\mathbf{THEN}\\ y_{i-1},\\\\\n        \\text{Rule}_{i}   & : &\\mathbf{IF}\\ x\\; is\\; A_{i  }\\ &\\mathbf{THEN}\\ y_{i},\\\\\n        \\text{Rule}_{i+1} & : &\\mathbf{IF}\\ x\\; is\\; A_{i+1}\\ &\\mathbf{THEN}\\ y_{i+1},\\\\\n    \\end{array}\n```\n\nwhere $y_{i}$ is an scalar. Mamdani inference is denoted as:\n\n```math\nOutput = \\frac{\\sum \\mu(x, A_{i})*y_{i}}{\\sum \\mu(x, A_{i})}\n```\n\nStraightforward implementation with `FuzzyLayer`:\n\n```python\nmamdani_fis = nn.Sequential(\n    FuzzyLayer.from_dimensions(input_dimention, fuzzy_rules_count, trainable=True),\n    nn.Softmax(1),\n    nn.Linear(fuzzy_rules_count, output_dimention, bias=False)\n    )\n```\n\nA more correct implementation is implemented in the `DefuzzyLinearLayer`, the network structure takes the following form\n\n```python\nmamdani_fis = nn.Sequential(\n            FuzzyLayer.from_dimensions(input_dimention, fuzzy_rules_count, trainable=True),\n            DefuzzyLinearLayer.from_dimensions(fuzzy_rules_count, output_dimention)\n        )\n```\n\n## Publications\n\n[Variational Autoencoders with Fuzzy Inference (Russian)](https://habr.com/ru/articles/803789/)\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Experiments with fuzzy layers and neural networks",
    "version": "0.0.2",
    "project_urls": null,
    "split_keywords": [
        "fuzzy"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "af185f439d323914a5f14102eb6250b54beb9b5689807c38991db575740d2f4a",
                "md5": "48f3ee944bcfb96f0098437c1b1bd200",
                "sha256": "909aee973ddac2927e150ecb592ad471e823e3b174ab41cc985237ae07f3cb9d"
            },
            "downloads": -1,
            "filename": "torchfuzzy-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "48f3ee944bcfb96f0098437c1b1bd200",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 7293,
            "upload_time": "2024-10-24T14:33:04",
            "upload_time_iso_8601": "2024-10-24T14:33:04.327418Z",
            "url": "https://files.pythonhosted.org/packages/af/18/5f439d323914a5f14102eb6250b54beb9b5689807c38991db575740d2f4a/torchfuzzy-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9731d5a9d70a8b34e9be179599caa3f245e3c299d88c8483e7373686b0a90ca5",
                "md5": "e4ac9be28991be4ae1f769cc141a177f",
                "sha256": "7ac59b7b74aaa64a29c6a1a722a059d6ae9b375d2910bdc16b4b70a1169847ab"
            },
            "downloads": -1,
            "filename": "torchfuzzy-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "e4ac9be28991be4ae1f769cc141a177f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 6005,
            "upload_time": "2024-10-24T14:33:05",
            "upload_time_iso_8601": "2024-10-24T14:33:05.568683Z",
            "url": "https://files.pythonhosted.org/packages/97/31/d5a9d70a8b34e9be179599caa3f245e3c299d88c8483e7373686b0a90ca5/torchfuzzy-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-24 14:33:05",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "torchfuzzy"
}
        
Elapsed time: 0.35642s