File size: 2,384 Bytes
0873620
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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)