본문 바로가기

implement_ml_models

implement_stride

2022.11.22 - [implement_ml_models] - implement_cnn

 

implement_cnn

합성곱 연산을 사용하는 신경망 row = [] matrix = [] for matrix_row in range(input.shape[0]-w.shape[0]+1): for matrix_col in range(input.shape[1]-w.shape[1]+1): matrix = [] for w_row in range(w.shape[0]): row = [] for w_col in range(w.shape[0]):

teach-meaning.tistory.com

스트라이드는 합성곱 연산 후 다음 계산 영역 선택 간격을 정하는 값

이전 합성곱 연산

result_arr = []

for matrix_row in range(input.shape[0]-w.shape[0]+1):
  for matrix_col in range(input.shape[1]-w.shape[1]+1):
    result = []
    for w_row in range(w.shape[0]):
      for w_col in range(w.shape[1]):
        result.append(input[matrix_row+w_row,matrix_col+w_col] * w[w_row, w_col])
    result_arr.append(np.sum(result))

 

matrix_row, matrix_col의 값에 따라 stride가 결정된다. 

result_arr = []
stride_row = 2
stride_col = 2

for matrix_row in range(0, input.shape[0]-w.shape[0]+1, stride_row):
  for matrix_col in range(0, input.shape[1]-w.shape[1]+1, stride_col):
    print(matrix_row, matrix_col)
    result = []
    for w_row in range(w.shape[0]):
      for w_col in range(w.shape[1]):
        result.append(input[matrix_row+w_row,matrix_col+w_col] * w[w_row, w_col])
    result_arr.append(np.sum(result))

matrix_row, matrix_col 의 for 문 증가값을 stride 값으로 지정하여 적용

result_arr
>>>
[0.9936819252205649,
 1.5208360953237963,
 1.3855064366261072,
 1.4757366500760771]

result = np.array(result_arr).reshape(int(input.shape[0]-w.shape[0]+1/2),-1)
result
>>>
array([[0.99368193, 1.5208361 ],
       [1.38550644, 1.47573665]])

 

'implement_ml_models' 카테고리의 다른 글

implement_differential  (0) 2022.11.23
implement_pooling  (0) 2022.11.22
implement_padding  (0) 2022.11.22
implement_cnn  (0) 2022.11.22
implement_activation  (0) 2022.11.21