import numpy as np # 하이퍼파라미터 설정 input_size = 2 # 입력층 노드 수 hidden_size = 3 # 은닉층 노드 수 output_size = 1 # 출력층 노드 수 learning_rate = 0.5 # 학습률 epochs = 10000 # 학습 반복 횟수 # 1. 데이터셋 정의 (XOR 문제) # 입력 데이터: [0,0], [0,1], [1,0], [1,1] X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) # 정답 레이블: [0], [1], [1], [0] y = np.array([[0], [1], [1], [0]]) # 2. 가중치(W)와 편향(b) 초기화 W1 = np.random.randn(input_size, hidden_size) b1 = np.random.randn(hidden_size) W2 = np.random.randn(hidden_size, output_size) b2 = np.random.randn(output_size) # 3. 활성화 함수와 그 미분 함수 정의 def sigmoid(x): return 1 / (1 + np.exp(-x)) def sigmoid_derivative(x): return x * (1 - x) # 4. 학습 시작 for epoch in range(epochs): # 순전파 (Forward Propagation) # 은닉층 hidden_output = np.dot(X, W1) + b1 hidden_activation = sigmoid(hidden_output) # 출력층 output_output = np.dot(hidden_activation, W2) + b2 predicted_output = sigmoid(output_output) # 역전파 (Backpropagation) # 1단계: 출력층의 오차와 기울기 계산 error_output = y - predicted_output delta_output = error_output * sigmoid_derivative(predicted_output) # 2단계: 은닉층의 오차와 기울기 계산 error_hidden = np.dot(delta_output, W2.T) delta_hidden = error_hidden * sigmoid_derivative(hidden_activation) # 3단계: 가중치와 편향 업데이트 W2 += np.dot(hidden_activation.T, delta_output) * learning_rate b2 += np.sum(delta_output, axis=0) * learning_rate W1 += np.dot(X.T, delta_hidden) * learning_rate b1 += np.sum(delta_hidden, axis=0) * learning_rate # 매 1000번째 epoch마다 오차 출력 if epoch % 1000 == 0: loss = np.mean(np.abs(error_output)) print(f"Epoch: {epoch}, Loss: {loss:.4f}") print("\n--- 학습 완료 ---") # 5. 학습된 모델로 예측 (결과 확인) hidden_output_final = np.dot(X, W1) + b1 hidden_activation_final = sigmoid(hidden_output_final) predicted_final = sigmoid(np.dot(hidden_activation_final, W2) + b2) print("입력 데이터:\n", X) print("예측 결과:\n", predicted_final.round()) print("정답 레이블:\n", y)