2023.02.11 - [분류 전체보기] - implement_MLP(parameter_update)(3)
implement_MLP(parameter_update)(3)
2023.01.31 - [implement_ml_models/MLP] - implement_MLP(parameter_update)(2) implement_MLP(parameter_update)(2) 2023.01.29 - [분류 전체보기] - implement_MLP(parameter_update) implement_MLP(parameter_update) MLP 모델에서 delta 값을 이용하여
teach-meaning.tistory.com
순전파 계산에 필요한 파라미터를 알아봤고, 클래스 내 함수를 통해 구현했다.
다음으론 비용 함수의 계산,
비용 함수에 필요한 값으로는
예측값과 타겟값으로 해당 값들에 대한 비용 함수의 계산을 통해 오차 값을 계산한다.
타겟 값(target) : 데이터의 타겟값
예측 값(predict) : 순전파 계산을 통해 얻은 예측 값
비용 함수(cost_function) : 예측 값과 타겟 값을 이용하여 비용함수의 값을 구한다.
class error_function:
def errer_squared_sum(self, predict, target):
error_cost = np.sum(0.5*((predict - target)**2))
return error_cost
class MLP:
input_data = []
weight = []
bias = []
node_input = []
node_output = []
predict = []
error_function1 = error_function()
activation1 = activation()
def forward_cal(self, input, w, b):
#가중치와 편향 값의 지정
self.w = w
self.b = b
#데이터 입력값이 입력층의 노드의 출력이 됨
output = input
self.node_output.append(output)
#3차원 배열 형태의 가중치의 층 갯수만큼 순전파 연산의 반복
for i in range(len(w)):
#행렬곱 연산을 위한 numpy_array 변환
w_numpy = np.array(w[i])
#가중치와 노드 출력의 행렬곱연산, 편향값 덧셈
hidden_input = w_numpy.T @ output + b[i][0]
#노드 입력과 활성화 함수 연산을 통한 노드 출력 계산
output = self.activation1.sigmoid(hidden_input)
self.node_output.append(output)
#최종 연산 결과, 예측값의 저장
self.predict = output
return output
def cost_function(self, target):
a = self.error_function1.errer_squared_sum(self.predict, target)
return a
정의한 error_function() 클래스에서 오차 제곱합을 호출,
예측값과 타겟값을 정의한 비용 함수에 넣어 해당 가중치에 대한 비용 함수의 값을 계산할 수 있다.
'implement_ml_models > MLP' 카테고리의 다른 글
implement_MLP(weight_update) (0) | 2023.02.17 |
---|---|
implement_MLP(parameter_update)(5 back propagation) (0) | 2023.02.15 |
implement_MLP(parameter_update)(3) (0) | 2023.02.11 |
implement_MLP(parameter_update)(2) (0) | 2023.01.31 |
implement_MLP(parameter_update) (0) | 2023.01.29 |