-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmodel.py
More file actions
38 lines (33 loc) · 1.36 KB
/
model.py
File metadata and controls
38 lines (33 loc) · 1.36 KB
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
import numpy as np
class AndModel:
def __init__(self):
# 파라메터
self.weights = np.random.rand(2)
self.bias = np.random.rand(1)
def train(self):
learning_rate = 0.1
epochs = 20
inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
outputs = np.array([0, 0, 0, 1])
for epoch in range(epochs):
for i in range(len(inputs)):
# 총 입력 계산
total_input = np.dot(inputs[i], self.weights) + self.bias
# 예측 출력 계산
prediction = self.step_function(total_input)
# 오차 계산
error = outputs[i] - prediction
print(f'inputs[i] : {inputs[i]}')
print(f'weights : {self.weights}')
print(f'bias before update: {self.bias}')
print(f'prediction: {prediction}')
print(f'error: {error}')
# 가중치와 편향 업데이트
self.weights += learning_rate * error * inputs[i]
self.bias += learning_rate * error
print('====')
def step_function(self, x):
return 1 if x >= 0 else 0
def predict(self, input_data):
total_input = np.dot(input_data, self.weights) + self.bias
return self.step_function(total_input)