|
|
import numpy as np
|
|
|
|
|
|
|
|
|
input_size = 2
|
|
|
hidden_size = 3
|
|
|
output_size = 1
|
|
|
learning_rate = 0.5
|
|
|
epochs = 10000
|
|
|
|
|
|
|
|
|
|
|
|
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
|
|
|
|
|
|
y = np.array([[0], [1], [1], [0]])
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
def sigmoid(x):
|
|
|
return 1 / (1 + np.exp(-x))
|
|
|
|
|
|
def sigmoid_derivative(x):
|
|
|
return x * (1 - x)
|
|
|
|
|
|
|
|
|
for epoch in range(epochs):
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
error_output = y - predicted_output
|
|
|
delta_output = error_output * sigmoid_derivative(predicted_output)
|
|
|
|
|
|
|
|
|
error_hidden = np.dot(delta_output, W2.T)
|
|
|
delta_hidden = error_hidden * sigmoid_derivative(hidden_activation)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
if epoch % 1000 == 0:
|
|
|
loss = np.mean(np.abs(error_output))
|
|
|
print(f"Epoch: {epoch}, Loss: {loss:.4f}")
|
|
|
|
|
|
print("\n--- ํ์ต ์๋ฃ ---")
|
|
|
|
|
|
|
|
|
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) |