Neuro-fuzzy systems (ANFIS): fuzzy rules that learn from data
A Sugeno fuzzy system as a five-layer network, hybrid learning with least squares and gradient, learning a function from noisy data, choosing the number of rules and overfitting, the readability of learned rules and the link to RBF networks.
Series AI basics Part 37 of 52
- Map of artificial intelligence: what is what and how it all fits together
- A brief history of artificial intelligence: from Turing to language models
- Data, model, training, inference: the basic terms on a single example
- How to read mathematical notation: symbols, indices, sums and quantifiers
- Sets and relations: the building blocks of definitions in AI
- Functions and mappings: why every AI model is a function
- Logic and proofs: the rules AI reasons by
- Graphs and trees: the structure that both search and neural networks stand on
- Linear algebra for machine learning: vectors, matrices and why AI mostly multiplies
- Derivatives, the gradient and the chain rule: how a model finds out which way to learn
- Gradient descent: how a model learns
- What probability is: from rolling dice to the Kolmogorov axioms
- Conditional probability and Bayes’ theorem: how to update your estimate when you learn something new
- Random variables and the cumulative distribution function: one function that tells you everything about a distribution
- Probability density and probability mass functions: why a continuous variable has zero probability at a point
- Expected value, variance and covariance: where a distribution lies, how wide it is and what goes with what
- Overview of probability distributions: Bernoulli, binomial, Poisson, uniform, normal and exponential
- Law of large numbers and central limit theorem: why averages behave so predictably
- Statistics and parameter estimation: maximum likelihood, MAP and confidence intervals
- Information theory: entropy, cross-entropy and KL divergence — where loss functions come from
- State-space search: BFS, DFS, UCS and A* with animations
- Games and adversaries: minimax, alpha-beta pruning and Monte Carlo tree search
- Constraint satisfaction: backtracking, constraint propagation and AC-3 on sudoku and a map of Czech regions
- Logic and knowledge systems: forward and backward chaining, resolution and expert systems
- Planning: STRIPS, the blocks world, heuristics from relaxation and planning in AI agents
- Local search and simulated annealing: a travelling salesman through the Czech regional capitals
- Genetic algorithms: population, crossover, mutation and selection on a backpack for a hike
- Evolution strategies and genetic programming: from the one-fifth rule to Kepler’s law
- Ant colony optimization: how a pheromone trail finds a short path
- Particle swarm optimisation: inertia, personal experience and the swarm’s experience
- Neuroevolution: evolving the weights and shape of neural networks, and the NEAT algorithm
- Fuzzy sets and membership functions: how to write down “somewhat warm”
- Fuzzy logic and probability: why “somewhat warm” does not mean “probably warm”
- Fuzzy logic: t-norms, implications and IF–THEN rules
- Fuzzy control: Mamdani and Sugeno from a fan to a pole on a cart
- Fuzzy clustering: the fuzzy c-means algorithm and points between clusters
- Neuro-fuzzy systems (ANFIS): fuzzy rules that learn from data
- Markov chains: the transition matrix, the stationary distribution and PageRank
- Hidden Markov models: the Viterbi algorithm and the dishonest casino
- Bayesian networks: why the grass is wet and what it means for the sprinkler
- The Kalman filter: how to estimate where a car is and how fast it goes from imprecise measurements
- Types of machine learning: supervised, unsupervised, semi-supervised and more
- Prompts, context and RAG: how to give a model the right information
- Variational autoencoders (VAE): new images from a continuous latent space
- Generative adversarial networks (GAN): the forger versus the detective
- Diffusion models: how pure noise becomes an image
- Markov decision processes: states, actions, rewards and the Bellman equations
- Q-learning and DQN: learning the value of actions, from a table to a neural network
- Policy gradient and PPO: learning the policy directly
- How an AI agent works: a language model in a loop with tools
- Programming with AI agents: the task, tests and review
- Evaluating and securing AI systems: how to measure and how to keep watch
Full series syllabus 52 of 83 topics published
- How a Sugeno fuzzy system is written as a network and which of its parameters are learned.
- Why hybrid learning (least squares plus gradient) outpaces pure gradient descent.
- Why adding rules risks overfitting and what is left of the readability of learned rules.
Useful to know first: Fuzzy control: Mamdani and Sugeno from a fan to a pole on a cart, Gradient descent: how a model learns
The fuzzy controllers from the previous parts had one weakness: someone had to come up with the rules and the membership functions. When an experienced operator exists, that is an advantage. When there is none — or when the system is supposed to reproduce measured data — it is a problem. Neural networks have the opposite nature: they learn anything from data, but nobody can read what they have learned. In 1993 Jyh-Shing Roger Jang combined the two in a system called ANFIS (Adaptive Neuro-Fuzzy Inference System): a Sugeno fuzzy system written as a network whose parameters are learned from data. This part closes the module on fuzzy logic.
It builds on the parts Fuzzy control: Mamdani and Sugeno and Gradient descent.
1. A fuzzy system as a network
Take a first-order Sugeno system with one input and four rules “IF x is Ai, THEN y = pix + ri”. Every step of its computation is a layer of a network:
Definition — ANFIS (Jang, 1993)
Let n ≥ 1 be the number of rules and for every i = 1, …, n let there be given premise parameters ci ∈ ℝ, σi > 0 and consequent parameters pi, ri ∈ ℝ. ANFIS with one input and n rules “IF x is Ai, THEN y = pix + ri” is the function y : ℝ → ℝ whose value at an input x ∈ ℝ is computed in five layers (for i = 1, …, n):
- Membership: μi(x) = exp(−(x − ci)2/2σi2) ∈ (0, 1] — the Gaussian membership function of the term Ai with centre ci and width σi.
- Firing strength: wi = μi(x) (with more inputs, the product of the memberships of all conditions of the rule).
- Normalization: w̄i = wi/∑j=1n wj (the denominator is positive, because every wj > 0).
- Consequent: w̄i · (pix + ri).
- Sum: y(x) = ∑i=1n w̄i (pix + ri).
So it is the output of a first-order Takagi–Sugeno controller with the product as the t-norm, only written as a network; layers 1 and 4 contain the learned parameters.
It is exactly the Sugeno controller from the previous part, only drawn as a network. Each rule has an “area of influence” (a Gaussian hill) and a linear prediction inside it. The output is a weighted average of the predictions, according to how much the input lies in each rule’s area. The whole system is differentiable, so it can be trained just like a neural network.
y = ∑i wi/∑j wj · (pix + ri)
- y
- the output of the whole system for input x — what we compare with the data and use to measure the error
- ∑i
- a sum over all rules — every rule contributes, none is “selected”; that is why the output is smooth and differentiable
- wi/∑j wj
- the normalized strength w̄i — dividing by the sum makes the weights add up to 1, so the output is a weighted average of the consequents and does not depend on how tall the hills are
- pix + ri
- the rule’s linear consequent — a local line describing the function in the rule’s area; a line instead of a constant lets a few rules follow the slope as well
μi(x) = exp( − (x − ci)2/2σi2 )
- μi(x)
- a membership degree in [0, 1] — how much x is “around ci”; the exponential of a negative number is always between 0 and 1, and at the centre it is exactly 1
- (x − ci)2
- the squared distance from the centre — membership falls off equally on both sides and stays smooth even at the centre (with an absolute value it would have a spike there and no derivative)
- 2σi2
- the width of the hill — distance is measured in units of σi, so at a distance σi from the centre the membership is always exp(−1/2) ≈ 0.61; learning σi changes how far the rule “reaches”
Example — a forward pass at x = 3
Take the system after epoch zero of learning (see below): centres c = 0, 2, 4, 6, all widths σ = 1 and consequents from least squares. For x = 3 the distances from the centres are 3, 1, 1, 3, and so
- membership and strength (colours as in the formula for μi): w1 = exp(−32/2 · 12) = e−4.5, w2 = exp(−12/2 · 12) = e−0.5 and symmetrically w3 = e−0.5, w4 = e−4.5; so w = (0.0111; 0.6065; 0.6065; 0.0111);
- normalization (colours from this step on as in the formula for y): the sum is 1.2353, so w̄ = (0.0090; 0.4910; 0.4910; 0.0090), e.g. w̄2 = 0.6065/1.2353;
- consequents: the lines pix + ri with the learned coefficients (9.54x + 0.01; 9.34x − 17.38; 8.56x − 36.61; 2.60x − 18.90) take the values (28.63; 10.66; −10.94; −11.09) at 3 — the coefficients are rounded, the values computed from the exact ones;
- sum of the products w̄i · (pix + ri): y = 0.2574 + 5.2329 − 5.3698 − 0.0997 ≈ 0.021.
The true value is sin(3) = sin(32/3) ≈ 0.141. Notice that the output comes from subtracting two large, nearly equal contributions — we will come back to that under readability.
2. Hybrid learning
We want to minimize the mean squared error on the training data. It could all be done by gradient descent. Jang, however, noticed that the output is linear in the consequent parameters.
Definition — mean squared error and RMSE
Let N ≥ 1, let (x1, y1), …, (xN, yN) ∈ ℝ × ℝ be data and ŷ : ℝ → ℝ a model (its output for input x is ŷ(x)). The mean squared error of the model ŷ on these data is the number
E = 1/N ∑k=1N (ŷ(xk) − yk)2 ≥ 0
and the RMSE (root mean square error) is its square root, RMSE = √E. Minimizing one or the other is the same thing; RMSE is reported because it has the units of the output. All errors in this part are RMSE.
RMSE = √1/N ∑k (ŷ(xk) − yk)2
- ŷ(xk)
- the system’s prediction at the k-th point — the only place where the parameters c, σ, p, r enter the error
- − yk, then squared
- the deviation from the measured value, squared — the sign does not matter, large errors weigh more than small ones, and the square is smoothly differentiable (and for the consequents allows an exact least-squares solution)
- 1/N ∑k
- an average over the points — the error on 40 training points and on 121 test points is then comparable
Algorithm — hybrid learning
With fixed premises, y(x) = ∑i ((w̄ix) pi + w̄i ri) is a linear function of the consequent parameters. This algorithm is built on that.
Input: training data (xk, yk) ∈ ℝ × ℝ, k = 1, …, N; the number of rules n; initial centres ci ∈ ℝ and widths σi > 0; a learning step η > 0; a number of epochs T ∈ ℕ; the smallest allowed width σmin > 0. Output: the parameters ci, σi, pi, ri, i = 1, …, n. For epochs t = 0, 1, …, T repeat:
- forward: compute w̄i(xk) for all i and k and find the consequent parameters pi, ri exactly by the least-squares method (definition below);
- stopping: if t = T, stop and return the parameters;
- backward: with the consequents found this way, take a gradient descent step for the centres and widths (gradients by the theorem below): ci ← ci − η ∂E/∂ci, σi ← max(σmin, σi − η ∂E/∂σi).
In the example in this part η = 0.1, T = 300 and σmin = 0.05.
Where a problem can be solved exactly, solve it exactly; leave the gradient only for what cannot be solved exactly. Linear regression from the part on statistics finds the best line in a single computation — and here it finds the best linear consequents of all rules at once. The gradient then only shifts and widens the hills of the membership functions.
Definition — least squares for the consequents
Let (xk, yk) ∈ ℝ × ℝ, k = 1, …, N, be the training data, n ≥ 1 the number of rules, and let the premise parameters ci, σi be fixed. Then the values w̄i(xk) are just numbers (w̄i for short). Arrange the consequent parameters into a vector θ = (p1, r1, …, pn, rn) ∈ ℝ2n and for each point k build the row
ak = (w̄1xk, w̄1, …, w̄nxk, w̄n) ∈ ℝ2n, so that ŷ(xk) = ak · θ.
The rows a1, …, aN form a matrix A ∈ ℝN×2n, and y = (y1, …, yN) ∈ ℝN. The least-squares solution is a vector
θ* ∈ argminθ ∈ ℝ2n ‖Aθ − y‖2/N,
i.e. the consequent parameters with the smallest mean squared error. Every such θ* satisfies the normal equations ATA θ = ATy — a system of 2n linear equations (and conversely every solution of them is a minimum; it is unique if and only if A has linearly independent columns). For 4 rules and 40 points, A is a 40 × 8 matrix and the system has 8 equations. Our computation also adds 10−6 to the diagonal of ATA (a small ridge term), so that the system is solvable even with nearly dependent columns, and solves it by Gaussian elimination.
Theorem — gradient with respect to centre and width
Let (xk, yk) ∈ ℝ × ℝ, k = 1, …, N, be data and ŷ an ANFIS with n rules and parameters ci ∈ ℝ, σi > 0, pi, ri ∈ ℝ. Let E be its mean squared error on the data, viewed as a function of all the parameters, and for an input x write wi = exp(−(x − ci)2/2σi2), W = ∑j wj, w̄i = wi/W and gi = pix + ri. Then for every i = 1, …, n
∂E/∂ci = 2/N ∑k=1N (ŷ − yk) (gi − ŷ) w̄i xk − ci/σi2,
∂E/∂σi = 2/N ∑k=1N (ŷ − yk) (gi − ŷ) w̄i (xk − ci)2/σi3,
where ŷ, gi and w̄i are taken at the point x = xk.
Proof. From ŷ = ∑j wjgj/W the quotient rule gives ∂ŷ/∂wi = giW − ∑j wjgj/W2 = gi − ŷ/W. The derivatives of a Gaussian hill are ∂wi/∂ci = wi x − ci/σi2 and ∂wi/∂σi = wi (x − ci)2/σi3, and the parameters ci, σi affect ŷ only through wi. By the chain rule ∂E/∂ci = 2/N ∑k (ŷ − yk) · gi − ŷ/W · wi xk − ci/σi2, and wi/W = w̄i gives the first formula; the second likewise. ∎
∂E/∂ci = 2/N ∑k (ŷ − yk) (gi − ŷ) w̄i xk − ci/σi2
- ∂E/∂ci
- how the error changes when we shift the centre of the i-th hill — the learning step goes against this sign
- ŷ − yk
- the error at the point — where the system hits the target, the point wants nothing and contributes nothing to the gradient
- gi − ŷ
- how the rule’s consequent differs from the overall output — giving the rule more weight pulls the output towards its consequent; when the consequent says the same as the whole, shifting the hill is pointless
- w̄i
- the rule’s share of the output — points far from the hill, where the rule has almost no say, barely affect its centre
- xk − ci/σi2
- the direction of the shift — a point to the right of the centre pulls the hill right, one to the left pulls it left; at the centre itself the hill is flat and a shift changes nothing
The whole learning from the example below in pseudocode (step η = 0.1, 300 epochs):
c = centres evenly spaced on [0, 6] # for 4 rules 0, 2, 4, 6
σ = half the spacing of the centres # for 4 rules 1
for epocha in 0 … 300:
# forward: layers 1–3, then the consequents exactly by least squares
wn[k][i] = w̄_i(x_k) for all points k and rules i
A = rows (wn[k][1]·x_k, wn[k][1], …, wn[k][n]·x_k, wn[k][n])
p, r = solve (AᵀA + 10⁻⁶·I)·θ = Aᵀy
if epocha == 300: break
# backward: a gradient step by the formulas above
gc, gσ = ∂E/∂c, ∂E/∂σ
c = c − η·gc
σ = max(0.05, σ − η·gσ) # the hill must not vanish
3. ANFIS learns a function
The task: from 40 noisy samples (noise with standard deviation 0.1), learn the function y = sin(x2/3) on the interval [0, 6]. The function oscillates faster and faster to the right, so four evenly spaced rules are not ideal. We also measure the error on 121 noise-free points outside the training data:
Right in epoch zero — with just least squares for the consequents and evenly spaced hills — the test error is 0.394 and the training error 0.198. Then the premises start learning. After 100 epochs the test error is 0.202 and after 300 epochs 0.173. On the training data it drops to 0.107 — below the noise level in the data (0.114); you cannot sensibly do better. The centres of the hills moved only a little; the main change is in the widths — the last rule on the right, where the function oscillates fastest, narrowed from 1 to 0.64.
Definition — noise level in the data
Let f : ℝ → ℝ be the true function, x1, …, xN ∈ ℝ the inputs, and let the training outputs be yk = f(xk) + εk, k = 1, …, N, where εk are random deviations (noise). The noise level in the data is the RMSE that the true function f itself would have on the training data as a model:
√1/N ∑k=1N (f(xk) − yk)2 = √1/N ∑k=1N εk2.
In our example N = 40, f(x) = sin(x2/3), and εk are independent random deviations from a normal distribution with mean 0 and standard deviation 0.1; the noise level came out as √1/40 ∑k εk2 = 0.114. It is slightly larger than 0.1 only by chance — in our 40 drawn deviations.
A model whose training error is smaller than the noise level (0.107 < 0.114) is already starting to copy the random deviations too. With such a small difference it does no harm yet — the test error keeps falling — but it is a first warning, which we will come back to when choosing the number of rules.
Pure gradient descent for all parameters starts from zero consequents and learns much more slowly: after 300 epochs its test error is 0.387, and even after 3,000 epochs (0.188) it does not catch up with hybrid learning after 300. A larger learning step, moreover, quickly leads to divergence. Splitting the problem into a linear and a nonlinear part pays off.
4. How many rules?
| Number of rules | 2 | 3 | 4 | 6 | 8 | 12 |
|---|---|---|---|---|---|---|
| Training error | 0.510 | 0.181 | 0.107 | 0.098 | 0.089 | 0.085 |
| Test error | 0.590 | 0.177 | 0.173 | 0.172 | 0.566 | 0.319 |
As rules are added, the training error keeps falling. On the test data, however, it rises sharply from eight rules on — the system has started learning the noise: with 40 points and 32 parameters (8 rules × 4) it has enough freedom to oscillate wildly between the training points. That is overfitting, the same phenomenon as with an over-bent polynomial regression. A sensible number of rules here lies between three and six.
Example — how many parameters ANFIS has
With one input, each rule has two premise parameters (ci, σi) and two consequent parameters (pi, ri), 4n in total. For 40 training points:
- 4 rules: 16 parameters, 8 of them linear — each linear parameter gets 40/8 = 5 points;
- 8 rules: 32 parameters, 16 of them linear — 40/16 = 2.5 points per linear parameter, so least squares has enough freedom to fit the noise as well;
- 12 rules: 48 parameters — more than there are points. Least squares determines “only” 24 consequents from 40 equations, but the other 24 premise parameters can move and narrow the hills exactly where the noise happened to stray.
A useful rule of thumb: there should be at least several times more points than parameters estimated from them.
5. What can be read
The promise of neuro-fuzzy systems is that the learned model stays readable. The premises are readable: four rules “around 0”, “around 2.2”, “around 4” and “around 6.2”. The consequents, however, are not. The rule “IF x is around 2.2, THEN y = 10.06 x − 13.83” would predict 8.25 at its centre, while the true value is 1.0. The individual rules balance each other out — the hills overlap a lot and the output only makes sense as a whole.
Example — how the rules balance each other at x = 2.195
The learned centre of the second rule is c2 = 2.195. There the second rule has full membership (w2 = 1), but its neighbours do not have zero: w = (0.140; 1; 0.331; 0.000), sum 1.471, so for example w̄2 = 1/1.471 ≈ 0.680 (colours as in the formula for y in the first section, gi = pix + ri):
| Rule | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| weight w̄i | 0.095 | 0.680 | 0.225 | 0.000 |
| consequent gi | 12.64 | 8.25 | −25.64 | −31.68 |
| contribution w̄igi | 1.203 | 5.609 | −5.769 | 0.000 |
The sum is y = 1.203 + 5.609 − 5.769 = 1.043, the true value sin(2.1952/3) = 0.999. The output is good, but it arises as the difference of two contributions of around ±5.7: the second rule “overshoots” upwards and the third pulls it back down. None of the rules on its own says what y is.
Interpretability, then, does not come for free. Constraints help to keep it: less overlap between membership functions, regularization of the consequents, or zero-order Sugeno with constant consequents. The price is paid in accuracy. It is the same balance as everywhere in machine learning — between what a model can do and what we can say about it.
6. ANFIS and neural networks
- Kinship with RBF networks. In 1993 Jang and Sun showed that radial basis function networks and zero-order Sugeno fuzzy systems are, under mild conditions, the same thing, only interpreted differently.
- The curse of dimensionality. With n inputs and k terms per input, the rule grid has kn rules — over 59 thousand for 10 inputs and 3 terms. That is why ANFIS is used for few inputs, or with rules chosen by clustering (for example fuzzy c-means from the previous part).
- Applications. Time series forecasting (the classic benchmark is the chaotic Mackey–Glass series), modelling and control of systems where we have measured data but little theory.
- Today. Deep networks have long since taken over tasks with many inputs. The idea of “trainable but readable” lives on in explainable AI, though — and fuzzy layers are occasionally added even to deep networks where the output needs to be explained by rules.
Definition — radial basis function (RBF) network
Let d ≥ 1 be the number of inputs, n ≥ 1 the number of hidden neurons, c1, …, cn ∈ ℝd centres, v1, …, vn ∈ ℝ output weights and φ : [0, ∞) → (0, ∞) a decreasing radial basis function (typically Gaussian, φ(t) = exp(−t2/2σ2) with σ > 0). A normalized RBF network is the function y : ℝd → ℝ,
y(x) = ∑i=1n vi φ(‖x − ci‖)/∑j=1n φ(‖x − cj‖),
where ‖·‖ is the Euclidean norm; the denominator is positive because φ has positive values. So the output is a weighted average of the output weights, where the weight of neuron i decreases with the distance of the input from its centre ci. A zero-order Sugeno system with Gaussian memberships and the product as “and” is y = ∑i w̄i ri — after substituting wi = φ(‖x − ci‖) and vi = ri, exactly the same formula. Put simply: rule = neuron, membership function = basis function, consequent = output weight. (Jang and Sun, 1993, list the conditions precisely: the same number of rules and neurons, constant consequents, Gaussian memberships with the same width within one rule, the product as “and”, and the same way of combining the output — weighted average or weighted sum.)
Example — the curse of dimensionality in numbers
With n inputs and k terms per input, the full grid has kn rules. A first-order Sugeno consequent y = p1x1 + … + pnxn + r has n + 1 parameters, so there are kn(n + 1) linear parameters. There are only 2kn premise parameters — each input has its own k hills, shared by all rules.
For n = 10 and k = 3: 310 = 59,049 rules, 59,049 · 11 = 649,539 consequent parameters and 2 · 3 · 10 = 60 premise parameters. Least squares would be solving a system with 649,539 unknowns — and far more data still would be needed.
7. Summary
- ANFIS is a Sugeno fuzzy system written as a five-layer network: membership, firing strength, normalization, consequent, sum.
- Hybrid learning finds the linear consequents exactly by least squares and fine-tunes the centres and widths of the membership functions by gradient.
- On the function sin(x2/3), learning the premises reduced the test error from 0.394 to 0.173; pure gradient needed ten times more epochs for a worse result.
- Too many rules lead to overfitting — from eight rules on, the test error rises sharply.
- The premises stay readable, but the consequents balance each other out; interpretability has to be watched.
This part closes the module on fuzzy logic: from the word “warm” through logic and controllers to a system that learns its rules by itself. Want to get hands-on? Have an AI agent train ANFIS on electricity consumption versus temperature from public data — and then look at which rules make sense.
Do you see it differently? Write to support@coden-app.com.