Daniel notes
you@here:~/articles/anfis-toolbox$ cat how-anfis-works.md

#anfis #fuzzy #theory

How ANFIS works

ANFIS, the Adaptive Neuro-Fuzzy Inference System, was proposed by Jang in 1993[1]. The idea is to write a fuzzy rule base as a network, so that it can be trained with the tools of neural networks while staying a rule base you can read.

A rule

Take two inputs, x1 and x2. A first-order TSK rule says:

if x1 is Ai and x2 is Bi then fi=pix1+qix2+ri

Ai and Bi are fuzzy sets. The part before then is the premise and decides where in the input space the rule applies. The part after it is the consequent: a small linear model that is valid in that region.

Four layers

flowchart LR
    X[inputs] --> M[1 membership]
    M --> R[2 rules]
    R --> N[3 normalization]
    N --> C[4 consequent]
    X --> C
    C --> Y[output]

Layer 1, membership. Each input is compared with each of its fuzzy sets. With a Gaussian set the result is

μAi(x)=exp(−(x−ci)22σi2)

a number between 0 and 1 that says how much x belongs to Ai. The centre ci and the width σi are the premise parameters.

Layer 2, rules. The firing strength of a rule is the product of the memberships in its premise:

wi=μAi(x1)μBi(x2)

Layer 3, normalization. Strengths are divided by their sum, so they become weights that add up to 1:

w¯i=wi∑kwk

Layer 4, consequent. Each rule proposes its linear answer, and the output is the weighted sum:

y=∑iw¯i(pix1+qix2+ri)

The coefficients pi, qi and ri are the consequent parameters.

Hybrid learning

The two kinds of parameter behave differently, and that is what makes ANFIS train well.

Look at the last formula with the premise parameters held fixed: the weights w¯i are then just numbers, and the output is linear in every pi, qi and ri. The best consequents for the current premises can be found in one step with least squares, with no iteration.

The premise parameters sit inside the exponentials, so they are nonlinear and need gradient descent.

Each training epoch therefore has two passes:

Pass Premise parameters Consequent parameters
forward fixed solved by least squares
backward gradient descent fixed

Tip

Because half of the parameters are solved exactly at every epoch, hybrid training usually needs far fewer epochs than plain gradient descent on everything.

ANFIS Toolbox has this rule as its hybrid trainers, and also plain SGD, Adam, RMSProp and particle swarm optimization for when you want to compare. See A first model to try it.

Where it stops working

Layer 2 multiplies one membership per input. With two or ten inputs that is fine. With hundreds, a product of many numbers smaller than 1 goes to zero, and the normalization of layer 3 breaks down. That problem, and what to do about it, is the subject of Why fuzzy systems choke on many features.


  1. J.-S. R. Jang, "ANFIS: adaptive-network-based fuzzy inference system," IEEE Transactions on Systems, Man, and Cybernetics, vol. 23, no. 3, pp. 665-685, 1993. doi:10.1109/21.256541 ↩︎