본문 바로가기

implement_ml_models/CNN

implement_CNN(MLP)

2023.03.09 - [분류 전체보기] - implement_CNN(MLP 전 까지)

 

implement_CNN(MLP 전 까지)

2023.03.09 - [분류 전체보기] - implement_CNN(수정) implement_CNN(수정) 2023.03.09 - [분류 전체보기] - implement_CNN(same_padding) def same_padding_cnn(self, input, filter_size, filter_count): # 각 필터별 연산 결과를 저장할 리

teach-meaning.tistory.com

이후 Flatten 된 결과를 가지고 MLP 연산을 수행할 수 있다. 이는 이전에 구현한 MLP 를 통해 구현,

layer 를 구현하기 위한 MLP 의 약간의 변화

  #순전파 계산
  def forward_cal(self, input, node_count):

    # 가중치의 임의 생성, node_count 개수만큼의 은닉(또는 출력) 노드가 존재한다.
    # (n,1) 개의 입력 값과 m 개의 노드 연결 (m, n) 크기의 가중치가 존재해야 한다.
    weight = np.random.rand(node_count, input.shape[0])

    #편향값의 임의 생성, 동일한 편향 값을 사용한다.
    bias = np.random.rand(1)

    #가중치와 노드 출력의 행렬곱연산, 편향값 덧셈
    hidden_input = weight @ input + bias
            
    #노드 입력과 활성화 함수 연산을 통한 노드 출력 계산
    # cnn 에선 0과 1 사이의 결과를 출력해야 하기 때문에 시그모이드 활성화 함수를 사용용
    output = self.activation.sigmoid(hidden_input)
            
    #노드 출력의 저장(델타 값의 계산을 위해)
    self.node_output.append(output)

1 : 은닉층 노드 개수에 맞는 임의의 가중치를 생성한다.

2 : 동일한 편향값의 사용, 

3 : 가중치와 입력값의 행렬 곱 연산 수행

4 : 순전파 연산과 그 결과의 저장

cnn 클래스내 mlp_forward_cal 함수에 해당 연산을 수행, 레이어에 저장하는 과정의 추가

  def mlp_forward_cal(self, node_count):
    self.layer_result.append(self.mlp.forward_cal(self.layer_result[self.layer-1], node_count))

    self.layer = self.layer +1
cnn.mlp_forward_cal(128)

cnn.layer_result[7].shape
>>>
(128, 1)

cnn.mlp_forward_cal(64)

cnn.layer_result[8].shape
>>>
(64, 1)

cnn.mlp_forward_cal(10)

cnn.layer_result[9].shape
>>>
(10, 1)

위 과정을 통해 10개의 결과(각 숫자별 분류 확률)을 출력하는 cnn 모델을 구현했다.

'implement_ml_models > CNN' 카테고리의 다른 글

implement_CNN(MLP_weight_update)  (0) 2023.03.12
implement_CNN(back_propagation)  (0) 2023.03.12
implement_CNN(MLP 전 까지)  (0) 2023.03.09
implement_CNN(수정)  (0) 2023.03.09
implement_CNN(same_padding)  (0) 2023.03.09