# TensorFlow 101A. Manual Convolution Calculation

The note is to describe how to calculate Convolution via manual or TensorFlow command.

1. TensorFlow convolution common commands:
1. y= np.convolve(x,h,”valid”) and y= np.convolve(h,x,”valid”)  are same…also true for “same”,”full” options.
2. from scipy import signal as sg

sg.convolve is using FFT which is faster than np.convolve for big matrix convolution

1. inverse = numpy.linalg.inv(x)
1.  One dimension with zero padding

When we apply kernel, we always use kernel’s inverse to multiply and sum. 1. One dimension without zero padding 1.  One dimension filter with multiple dimensions of input

x= [[255,   7,  3],

[212, 240,  4],

[218, 216, 230],]

h= [[-1,1]]

y=np.convolve( x, h, ‘valid’)

[[248   4]

[-28 236]

[  2 -14]]

1. two dimension filter with with multiple dimensions of input

x= [[255,   7,  3],

[212, 240,  4],

[218, 216, 230],]

h= [ [-1 1] , [2 3] ]

y=np.convolve( x, h, ‘valid’)

—- better to reference matlab  https://www.mathworks.com/help/vision/ref/2dconvolution.html

How to compute the (1,1) output element (zero-based indexing) using these steps:

1. Rotate the second input matrix, I2, 180 degrees about its center element.
2. Slide the center element of I2 so that it lies on top of the (0,0) element of I1.
3. Multiply each element of the rotated I2 matrix by the element of I1 underneath.
4. Sum the individual products from step 3. 1. using tensorflow:
1. Numpy is great because it has high optmized matrix operations implemented in a backend using C/C++.

However, if our goal is to work with DeepLearning, we need much more. TensorFlow does the same work, but instead of returning to Python everytime,

it creates all the operations in the form of graphs and execute them once with the highly optimized backend.

1. Suppose that you have two tensors:

3×3 filter (4D tensor = [3,3,1,1] = [width, height, channels, number of filters])

10×10 image (4D tensor = [1,10,10,1] = [batch size, width, height, number of channels]

The output size for zero padding ‘SAME’ mode will be:

the same as input = 10×10

The output size without zero padding ‘VALID’ mode:

input size – kernel dimension +1 = 10 -3 + 1 = 8 = 8×8

1. using below for convolution:

arr= array([[ 64,  71,  64, …,  49,  47,  48],

[ 68,  71,  63, …,  53,  52,  51],

[ 65,  69,  65, …,  54,  50,  55],

…,

[ 21,  23,  22, …, 183, 169, 154],

[ 17,  20,  25, …, 179, 165, 152],

[ 26,  22,  20, …, 170, 159, 149]]

size of arr is 10×10

kernel = np.array([

[ 0, 1, 0],

[ 1,-4, 1],

[ 0, 1, 0],

])

size of kernel is 3×3

grad = signal.convolve2d(arr, kernel, mode=’same’, boundary=’symm’)