Exercise 1:

Calculate the 2x2 MaxPooling in the same manner as in the following example:

M =  [[ 12  20  30   0]
      [  8  12   2   0]
      [ 34  70  37   4]
      [112 100  25  12]]

result:

     [[ 20  30]
      [112  37]]

Reference Link

In [2]:
import numpy as np

a = [[ 12,  20, 30,  0,  7,  6],
     [  8,  12,  2,  0,  5,  3],
     [ 34,  70, 37,  4, 10, 12],
     [112, 100, 25, 12,  1,  3]]
A = np.array(a)
d = np.array([])
n = A.shape[0]
m = A.shape[1]

for i in range(0,n,2):
    for j in range(0,m,2):
        d = np.append(d,A[i:i+2,j:j+2].max())
print(d.reshape((2,3)))


# c1 = A[0:2,0:2].max()
# c2 = A[0:2,2:4].max()
# c3 = A[0:2,4:6].max()
#
# c4 = A[2:4,0:2].max()
# c5 = A[2:4,2:4].max()
# c6 = A[2:4,4:6].max()

# d = np.array([[c1,c2,c3],
#               [c4,c5,c6]])
[[ 20.  30.   7.]
 [112.  37.  12.]]

Exercise 2:

Calculate the 2x2 MinPooling in the same manner as the following example:

M =  [[ 12  20   30   0]
      [  8  12   2    0]
      [ 34  70   37   4]
      [112  100  25  12]]

result:

     [[ 8   0]
      [34   4]]
In [4]:
import numpy as np

a = [[ 12,  20, 30,  0,  7,  6],
     [  8,  12,  2,  0,  5,  3],
     [ 34,  70, 37,  4, 10, 12],
     [112, 100, 25, 12,  1,  3]]
A = np.array(a)
d = np.array([])
n = A.shape[0]
m = A.shape[1]

for i in range(0,n,2):
    for j in range(0,m,2):
        d = np.append(d,A[i:i+2,j:j+2].min())

print(d.reshape((2,3)))
[[ 8.  0.  3.]
 [34.  4.  1.]]

Exercise 3:

Calculate the 2x2 AvgPooling in the same manner as the following example:

M =  [[ 12  20  30   0]
      [  8  12   2   0]
      [ 34  70  37   4]
      [112 100  25  12]]

result:

     [[13.0   8.0]
      [79.0  19.5]]
In [6]:
import numpy as np

a = [[ 12,  20, 30,  0,  7,  6],
     [  8,  12,  2,  0,  5,  3],
     [ 34,  70, 37,  4, 10, 12],
     [112, 100, 25, 12,  1,  3]]
A = np.array(a)
d = np.array([])
n = A.shape[0]
m = A.shape[1]

for i in range(0,n,2):
    for j in range(0,m,2):
        d = np.append(d,A[i:i+2,j:j+2].mean())

print(d.reshape((2,3)))
[[13.    8.    5.25]
 [79.   19.5   6.5 ]]

Exercise 4:

Calculate the 2xk MaxPooling in the same manner as the following example:

M =  [[ 12  20  30   0   7   6]
      [  8  12   2   0   5   3]
      [ 34  70  37   4  10  12]
      [112 100  25  12   1   3]]

result:

     [[ 20  30  7]
      [ 12   2  5]
      [ 70  37 12]
      [112  25  3]]
In [9]:
import numpy as np

a = [[ 12,  20, 30,  0,  7,  6],
     [  8,  12,  2,  0,  5,  3],
     [ 34,  70, 37,  4, 10, 12],
     [112, 100, 25, 12,  1,  3]]
A = np.array(a)
d = np.array([])
n = A.shape[0]
m = A.shape[1]

y = int(input("kernel size1 = "))
k = int(input("kernel size2  = "))


if n%y == 0 and m%k == 0:
    for i in range(0,n,y):
        for j in range(0,m,k):
            d = np.append(d,A[i:i+y,j:j+k].max())
    t1 = int(n/y)
    t2 = int(m/k)
    d = d.reshape((t1,t2))
    print(d)
else:
    print(y,k,"is not the common divisor of",n,m)
kernel size1 = 2
kernel size2  = 2
[[ 20.  30.   7.]
 [112.  37.  12.]]

Exercise 5:

Pad 4 zeros rows and 4 zeros columns in the matrix borders:

A =  [[ 3 3 2 1 5]
      [ 5 0 1 3 1]
      [ 3 1 2 2 3]
      [ 2 0 0 2 2]
      [ 2 1 2 1 1]]

result:

A =  [[0 0 0 0 0 0 0 0 0]
      [0 0 0 0 0 0 0 0 0]
      [0 0 3 3 2 1 5 0 0]
      [0 0 5 0 1 3 1 0 0]
      [0 0 3 1 2 2 3 0 0]
      [0 0 2 0 0 2 2 0 0]
      [0 0 2 1 2 1 1 0 0]
      [0 0 0 0 0 0 0 0 0]
      [0 0 0 0 0 0 0 0 0]]
In [11]:
import numpy as np

a = [[ 3, 3, 2, 1, 5],
     [ 5, 0, 1, 3, 1],
     [ 3, 1, 2, 2, 3],
     [ 2, 0, 0, 2, 2],
     [ 2, 1, 2, 1, 1]]
A = np.array(a)
n = A.shape[0]
m = n + 4

b = np.zeros((m,m))
B = np.array(b)
B[2:7,2:7] = a
print(B)
[[0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 3. 3. 2. 1. 5. 0. 0.]
 [0. 0. 5. 0. 1. 3. 1. 0. 0.]
 [0. 0. 3. 1. 2. 2. 3. 0. 0.]
 [0. 0. 2. 0. 0. 2. 2. 0. 0.]
 [0. 0. 2. 1. 2. 1. 1. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0.]]

Exercise 6:

Remove zeros from the matrix borders:

M =   [[0 0 0 0 0 0 0]
       [0 3 3 2 1 0 0]
       [0 0 0 1 3 1 0]
       [0 3 1 2 2 3 0]
       [0 2 0 0 2 2 0]
       [0 2 0 0 0 1 0]
       [0 0 0 0 0 0 0]]

result:

M =   [[ 3 3 2 1 0]
       [ 0 0 1 3 1]
       [ 3 1 2 2 3]
       [ 2 0 0 2 2]
       [ 2 0 0 0 1]]
In [12]:
import numpy as np

a = [[0, 0, 0, 0, 0, 0, 0],
     [0, 3, 3, 2, 1, 0, 0],
     [0, 0, 0, 1, 3, 1, 0],
     [0, 3, 1, 2, 2, 3, 0],
     [0, 2, 0, 0, 2, 2, 0],
     [0, 2, 0, 0, 0, 1, 0],
     [0, 0, 0, 0, 0, 0, 0]]

A = np.array(a)
n = A.shape[0]
# A = np.delete(A,(0,n-1),axis=0)   # delete the 1st & the last rows
# m = A.shape[1]
# A = np.delete(A,(0,m-1),axis=1)   # # delete the 1st & the last columns
# print(A)

# 2nd way
A = A[1:n-1,1:n-1]
print(A)
[[3 3 2 1 0]
 [0 0 1 3 1]
 [3 1 2 2 3]
 [2 0 0 2 2]
 [2 0 0 0 1]]

Exercise 7:

Calculate the matrices multiplication and sum the result.

A =   [[1 0 1]
       [0 1 0]
       [0 0 1]]

B =   [[1 2 0]
       [0 2 0]
       [1 0 0]]

result:

AB =  [[2 2 0]
       [0 2 0]
       [1 0 0]]
In [13]:
import numpy as np

a = [[1, 0, 1],
     [0, 1, 0],
     [0, 0, 1]]

b = [[1, 2, 0],
     [0, 2, 0],
     [1, 0, 0]]

C = np.zeros((3,3))

A = np.array(a)
B = np.array(b)

n = A.shape[0]       # a的行
m = B.shape[1]       # b的列
t = A.shape[1]       # a的列 或 b的行

for i in range(n):       # 行数
    for j in range(m):   # 列数
        for k in range(t):
            C[i][j] = C[i][j] + (A[i][k] * B[k][j])
print(C)
print("sum = ",C.sum())

# x = A @ B
# print(x)
[[2. 2. 0.]
 [0. 2. 0.]
 [1. 0. 0.]]
sum =  7.0

Exercise 8:

Calculate the matrices multiplication and sum the result in this way:

A =  [[1 0 1 2]
      [0 1 0 0]
      [0 0 1 1]
      [1 0 1 1]]

B =  [[1 1]
      [0 2]]

result:

     [[4 6]
      [2 8]]
In [15]:
import numpy as np

a = [[1, 0, 1, 2],
     [0, 1, 0, 0],
     [0, 0, 1, 1],
     [1, 0, 1, 1]]

b = [[1, 1],
     [0, 2]]

A = np.array(a)
B = np.array(b)
M = np.array([])
C = A[:,0:2]
D = A[:,2:4]

x = C @ B
y = D @ B

Z = np.hstack((x,y))
n = A.shape[0]
m = A.shape[1]

for i in range(0,n,2):
    for j in range(0,m,2):
        M = np.append(M,Z[i:i+2,j:j+2].sum())
print(M.reshape((2,2)))
[[4. 6.]
 [2. 8.]]

Exercise 9:

Calculate the 2x2 Convolution as the following example:

M =   [[ 3 3 2 1 0]
       [ 0 0 1 3 1]
       [ 3 1 2 2 3]
       [ 2 0 0 2 2]
       [ 2 0 0 0 1]]

result:

      [[ 6 6 7 5]
       [ 4 4 8 9]
       [ 6 3 6 9]
       [ 4 0 2 5]]

Reference Link

In [16]:
import numpy as np

a = [[ 3, 3, 2, 1, 0],
     [ 0, 0, 1, 3, 1],
     [ 3, 1, 2, 2, 3],
     [ 2, 0, 0, 2, 2],
     [ 2, 0, 0, 0, 1]]
A = np.array(a)
d = np.array([])
n = A.shape[0]
m = A.shape[1]

for i in range(0,n-1,1):
    for j in range(0,m-1,1):
        d = np.append(d,A[i:i+2,j:j+2].sum())
print(d.reshape((n-1,m-1)))
[[6. 6. 7. 5.]
 [4. 4. 8. 9.]
 [6. 3. 6. 9.]
 [4. 0. 2. 5.]]

Exercise 10:

Calculate the 3x3 Convolution as the following example:

M =  [[ 3 3 2 1 0]
      [ 0 0 1 3 1]
      [ 3 1 2 2 3]
      [ 2 0 0 2 2]
      [ 2 0 0 0 1]]

result:

     [[ 15 15 15]
      [  9 11 16]
      [ 10  7 12]]
In [17]:
import numpy as np

a = [[ 3, 3, 2, 1, 0],
     [ 0, 0, 1, 3, 1],
     [ 3, 1, 2, 2, 3],
     [ 2, 0, 0, 2, 2],
     [ 2, 0, 0, 0, 1]]
A = np.array(a)
d = np.array([])
n = A.shape[0]
m = A.shape[1]

for i in range(0,n-2,1):
    for j in range(0,m-2,1):
        d = np.append(d,A[i:i+3,j:j+3].sum())
print(d.reshape((n-2,m-2)))
[[15. 15. 15.]
 [ 9. 11. 16.]
 [10.  7. 12.]]

Exercise 11:

Calculate the 2x3 Convolution as the following example:

M =   [[ 3 3 2 1 0]
       [ 0 0 1 3 1]
       [ 3 1 2 2 3]
       [ 2 0 0 2 2]
       [ 2 0 0 0 1]]

result:

      [[ 9  10   8]
       [ 7   9  12]
       [ 8   7  11]
       [ 4   2   5]]
In [18]:
import numpy as np

a = [[ 3, 3, 2, 1, 0],
     [ 0, 0, 1, 3, 1],
     [ 3, 1, 2, 2, 3],
     [ 2, 0, 0, 2, 2],
     [ 2, 0, 0, 0, 1]]

A = np.array(a)
d = np.array([])
n = A.shape[0]
m = A.shape[1]

for i in range(0,n-1,1):
    for j in range(0,m-2,1):
        d = np.append(d,A[i:i+2,j:j+3].sum())
print(d.reshape((n-1,m-2)))
[[ 9. 10.  8.]
 [ 7.  9. 12.]
 [ 8.  7. 11.]
 [ 4.  2.  5.]]

Exercise 12:

Calculate the 3x3 Convolution as the following example:

M =   [[ 3  3  2  1  0]
       [ 0  0  1  3  1]
       [ 3  1  2  2  3]
       [ 2  0  0  2  2]
       [ 2  0  0  0  1]]

result:

      [[ 6  9 10  8  5]
       [10 15 15 15 10]
       [ 6  9 11 16 13]
       [ 8 10  7 12 10]
       [ 4  4  2  5  5]]

Hint:

Before you do the convolution, try to pad two zero columns and two zero columns , like the following:

notes:

(result.sahpe =(5,5), it means the original shpe should be(5+(3-1),5+(3-1))

In [19]:
import numpy as np

a = [[ 3, 3, 2, 1, 0],
     [ 0, 0, 1, 3, 1],
     [ 3, 1, 2, 2, 3],
     [ 2, 0, 0, 2, 2],
     [ 2, 0, 0, 0, 1]]
A = np.array(a)
B = np.zeros((7,7))
C = np.array([])

n = B.shape[0]
m = B.shape[1]
B[1:n-1,1:m-1] = A

for i in range(0,n-2,1):
    for j in range(0,m-2,1):
        C = np.append(C,B[i:i+3,j:j+3].sum())
print(C.reshape((n-2,m-2)))
[[ 6.  9. 10.  8.  5.]
 [10. 15. 15. 15. 10.]
 [ 6.  9. 11. 16. 13.]
 [ 8. 10.  7. 12. 10.]
 [ 4.  4.  2.  5.  5.]]

Exercise13:

Calculate the 3x3 Convolution as the following example:

M =   [[ 3  3  2  1  0]
       [ 0  0  1  3  1]
       [ 3  1  2  2  3]
       [ 2  0  0  2  2]
       [ 2  0  0  0  1]]

result:

      [[ 6  9 10  8  5]
       [10 15 15 15 10]
       [ 6  9 11 16 13]
       [ 8 10  7 12 10]
       [ 4  4  2  5  5]]

hint:

Before you do the convolution, try to pad two zero columns and two zero columns.
like the following:

In [20]:
import numpy as np

a = [[ 3, 3, 2, 1, 0],
     [ 0, 0, 1, 3, 1],
     [ 3, 1, 2, 2, 3],
     [ 2, 0, 0, 2, 2],
     [ 2, 0, 0, 0, 1]]

w = [[1,0,1],
     [1,1,0],
     [1,0,0]]

W = np.array(w)
A = np.array(a)
B = np.zeros((7,7))
C = np.array([])
D = np.array([])

n = B.shape[0]
m = B.shape[1]
B[1:n-1,1:m-1] = A

for i in range(0,n-2,1):
    for j in range(0,m-2,1):
        C = np.append(C,(B[i:i+3,j:j+3]*W).sum())
C = C.reshape((5,5))    # a.shape=(7,7) reshape(7-(3-1),7-(3-1))
print(C)
[[ 3.  6.  5.  4.  4.]
 [ 3.  8.  6.  8.  7.]
 [ 3.  7.  6.  6. 10.]
 [ 3.  9.  3.  7.  6.]
 [ 2.  4.  2.  2.  3.]]