(python) Convolutional Neural Networks: Step by Step - Convolution

Coursera 참고 예제) Convolutional Neural Network 1주차. Convolutional Neural Networks: Step by Step. CNN구현


Convolution

image image image

import numpy as np
import h5py
import matplotlib.pyplot as plt

%matplotlib inline
plt.rcParams['figure.figsize'] = (5.0, 4.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

%load_ext autoreload
%autoreload 2

np.random.seed(1)

# GRADED FUNCTION: conv_single_step

def conv_single_step(a_slice_prev, W, b):
    # a_slice_prev : 이전 단계에서 활성화 함수를 거친 결과 (f,f,채널수)
    # W : 가중치 (f,f,이전 단계 채널 수)
    # b : bias (1,1,1)
    s = np.multiply(a_slice_prev, W) + b  
    
    # Sum over all entries of the volume s
    # Z : a scalar value, result of convolving the sliding window (W, b) on a slice x of the input data
    Z = np.sum(s)
    return Z
    
np.random.seed(1)
a_slice_prev = np.random.randn(4, 4, 3)
W = np.random.randn(4, 4, 3)
b = np.random.randn(1, 1, 1)

Z = conv_single_step(a_slice_prev, W, b)
print("Z =", Z)    
>> Z = -23.16021220252078