A membership function is a method of translating a crisp value
where
To make these MFs, we will use the FuzzyTorch library.
import os; os.chdir("../")
from functools import partial
import torch
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns
# Our Membership Functions import
from src.functional.membership import *
x = torch.linspace(0, 100, 100).view(-1, 1)
def draw_function(func):
sns.lineplot(x.flatten().numpy(), func.flatten())
plt.xlabel("$$x_i$$")
plt.ylabel("Membership Value")
plt.grid()
Triangluar Function
A triangular MF is created using three parameters
help(triangle)
Help on function triangle in module src.functional.membership:
triangle(x, a, b, c)
Triangular Membership Function
:param x: input value
:param a: start point where membership is 0
:param b: center point where membership is 1
:param c: end point where membership is 0
tri = partial(triangle, a=20, b=60, c=80)
draw_function(tri(x))
plt.text(22, tri(20), "a")
plt.text(62, tri(60), "b")
plt.text(82, tri(80), "c")
Text(82, tensor([0.]), ‘c’)
Trapezoid Membership Function
help(trapezoid)
Help on function trapezoid in module src.functional.membership:
trapezoid(x, a, b, c, d)
Trapezoidal Membership Function
:param x: input value
:param a: bottom left point where membership is 0
:param b: top left point where membership is 1
:param c: top right point where membership is 1
:param d: bottom right point where membership is 0
trap = partial(trapezoid, a=10, b=20, c=60, d=95)
draw_function(trap(x))
plt.text(11, trap(10), "a")
plt.text(21, trap(20), "b")
plt.text(61, trap(60), "c")
plt.text(96, trap(95), "d")
Text(96, tensor([0.]), ‘d’)
Gaussian Membership Function
help(gaussian)
Help on function gaussian in module src.functional.membership:
gaussian(x, a, b)
Gaussian Membership Function
:param x: input value
:param a: The mean of the Gaussian Distribution
:param b: The standard deviation of the Distribution
Usage: gaussian(40, a=50, b=20)
gaussian(torch.Tensor([[20],[30]]), a=50, b=20)
gaus = partial(gaussian, a=50, b=20)
draw_function(gaus(x))
plt.text(50-5, gaus(50)-0.1, "Mean")
plt.text(50+22, gaus(50+20), "Standard Deviation")
Text(72, tensor([0.6065]), ‘Standard Deviation’)
General Bell Curve Membership Function
help(bell)
Help on function bell in module src.functional.membership:
bell(x, a, b, c)
General Bell Curve Membership Function
:param x: input value
:param a: width of bell curve.
:param b: slop of the curve, lower values = curvier
:param c: centre of the curve.
bellf = partial(bell, a=20, b=4, c=50)
draw_function(bellf(x))
Sigmoidal Membership Function
help(sigmoid)
Help on function sigmoid in module src.functional.membership:
sigmoid(x, a, b)
Sigmoidal Membership Function
:param x: input value
:param a: amount of curvature, higher values = unit step
:param b: 0.5 centre posistion
sig = partial(sigmoid, a=1, b=50)
draw_function(sig(x))
Left-Right (LR) Membership Function
where
help(lr)
Help on function lr in module src.functional.membership:
lr(x, a, b, c)
Left-Right (LR) Membership Function
:param x: input value
:param a: centre point of change
:param b: rate of decay after change
:param c: length of decay
lr1 = partial(lr, a=65, b=60, c=10)
lr2 = partial(lr, a=25, b=10, c=40)
draw_function(lr1(x))
draw_function(lr2(x))
plt.grid()
plt.legend(["a = 65, b = 60, c = 10", "a = 25, b = 10, c = 40"])
<matplotlib.legend.Legend at 0x7f37d3458048>
Multi-Dimensional Functions
The combination of different functions can be applied to many inputs. Here, we shall consider two variables
# Our two variables
x = torch.linspace(-5, 5, 50)
y = torch.linspace(-5, 5, 50)
The single dimension function can be referred to as a
fig = plt.figure()
draw_function(gaussian(x, 0, 1.5))
plt.title("Base set of A")
Text(0.5, 1.0, ‘Base set of A’)
This can be turned into a cylindrical extension through:
where c(A) is our cylindrical extension.
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
xx, yy = np.meshgrid(x.numpy(), y.numpy())
ax.plot_surface(xx,
yy,
gaussian(torch.Tensor(xx), 0., 1.5).numpy(),
cmap="jet")
ax.set_xlabel("x input")
ax.set_ylabel("y input")
ax.set_zlabel("Membership Value")
Text(0.5, 0, ‘Membership Value’)
To use AND and OR operations, we can use the min and max respectively of two MF functions for each input dimension. The logical `and` is:
and the logical `or` is:
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
xx, yy = np.meshgrid(x.numpy(), y.numpy())
andOp = torch.max(gaussian(torch.Tensor(xx), 0, 1.5), gaussian(torch.Tensor(yy), 0, 1.5))
ax.plot_surface(xx, yy, andOp.numpy(), cmap="jet")
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
orOp = torch.min(gaussian(torch.Tensor(xx), 0, 1.5), gaussian(torch.Tensor(yy), 0, 1.5))
ax.plot_surface(xx, yy, orOp.numpy(), cmap="jet")
Citation
@online{morgan2019,
author = {Morgan, Jay Paul},
title = {Fuzzy {Logic} {Membership} {Functions}},
date = {2019-06-27},
url = {https://morganwastaken.com/blog/2019-06-27-membership-functions},
langid = {en}
}