Computational Foundations of Cognitive Science: Lecture 15: Convolutions and Kernels
Computational Foundations of Cognitive Science: Lecture 15: Convolutions and Kernels
Frank Keller
School of Informatics
University of Edinburgh
[email protected]
Definition
Definition: Convolution
If f and g are discrete functions, then f ∗ g is the convolution of f
and g and is defined as:
+∞
X
(f ∗ g )(x) = f (u)g (x − u)
u=−∞
Definition
Convolution of Vectors
Convolution of Vectors
Convolution of Vectors
Example
1 0
Assume a = 0 , b = 1.
−1 2
Then
a1 b1 1·0 0
a1 b2 + a2 b1
1·1+0·0 1
a∗b= a1 b3 + a2 b2 + a3 b1 = 1 · 2 + 0 · 1 + (−1)0 = 2 .
a2 b3 + a3 b2 0 · 2 + (−1)1 −1
a3 b3 (−1)2 −2
Mid-lecture Problem
What happens if the kernel is smaller than the input vector (this is
actually the typical case)?
1
1
1
−2 1
−1. Compute a ∗ b.
Assume a = 1 , b =
2
−1
−1
Convolution of Matrices
Convolution of Matrices
We can regard functions of two variables as matrices with
Axy = f (x, y ), and obtain a matrix definition of convolution.
where u and v range over all legal subscripts for auv and
bx−u+1,y −v +1 .
Convolution of Matrices
Example
b11 b12 b13 b14 b15
a11 a12 a13 b21 b22 b23 b24 b25
Let A = a21 a22 a23 and B =
b31
.
b32 b33 b34 b35
a31 a32 a33
b41 b42 b43 b44 b45
Then for C = A ∗ B, the entry c33 = a11 b33 + a12 b32 + a13 b31 +
a21 b23 + a22 b22 + a23 b21 + a31 b13 + a32 b12 + a33 b11 .
Here, B could represent an image, and A could represent a kernel
performing an image operation, for instance.
B= K ∗B =
1/9 1/9 1/9
K = 1/9 1/9 1/9 implements a mean filter which smooths an
1/9 1/9 1/9
image by replacing each pixel value with the mean of its neighbors.
Frank Keller Computational Foundations of Cognitive Science 12
Definition
Convolutions of Discrete Functions Convolution of Vectors
Convolutions of Continuous Functions Mid-lecture Problem
Convolution of Matrices
B= K ∗B =
1 0 −1
The kernel K = 2 0 −2 implements the Sobel edge detector.
1 0 −1
It detects gradients in the pixel values (sudden changes in brightness),
which correspond to edges. The example is for vertical edges.
Definition
Definition
Example
Assume the following step functions:
1
3 if 0 ≤ x ≤ 4 2 if − 1 ≤ x ≤ 1
g (x) = f (x) =
0 otherwise 0 otherwise
0 if x ≤ 0
If we integrate g (x), we get: G (x) = 3x if 0 ≤ x ≤ 4 .
12 if x > 4
Then the convolution f ∗ g is:
R +∞ R1
(f ∗ g )(x) = −∞ f (u)g (x − u)du = 21 −1 g (x − u)du =
R x−1
− 21 x+1 g (u)du = − 12 (G (x − 1) − G (x + 1)) =
3
(x + 1) if − 1 ≤ x < 1
2
3 if 1 ≤ x ≤ 3
3
2
− (x − 1) + 6 if 3<x ≤5
0 otherwise
Definition
Function g (x) (blue) and convolution f ∗ g (x) (green):
4
3.5
2.5
1.5
0.5
−0.5
−1
−2 −1 0 1 2 3 4 5 6
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
−3 −2 −1 0 1 2 3
Frank Keller Computational Foundations of Cognitive Science 18
Convolutions of Discrete Functions Definition
Convolutions of Continuous Functions Example: Signal Processing
200
180
160
140
120
100
80
60
40
20
0
0 20 40 60 80 100 120 140 160 180 200
Frank Keller Computational Foundations of Cognitive Science 19
Convolutions of Discrete Functions Definition
Convolutions of Continuous Functions Example: Signal Processing
200
180
160
140
120
100
80
60
40
20
0
0 20 40 60 80 100 120 140 160 180 200
Frank Keller Computational Foundations of Cognitive Science 20
Convolutions of Discrete Functions Definition
Convolutions of Continuous Functions Example: Signal Processing
Summary
P
The convolution (f ∗ g )(x) = f (u)g (x − u) represents the
overlap between a discrete function g and a kernel f ;
convolutions in one dimension can be represented as vectors,
convolutions in two dimensions as matrices;
in image processing, two dimensional convolution can be used
to filter an image or for edge detection;
for continuousR functions, convolution is defined as
(f ∗ g )(x) = f (u)g (x − u)du;
this can be used in signal processing, e.g., to smooth a signal.