Python 데이터 분석
Python 데이터분석 기초 71 - Perceptron(퍼셉트론, 단층신경망)
코딩탕탕
2022. 11. 25. 12:42
Perceptron(퍼셉트론, 단층신경망)
Perceptron(퍼셉트론, 단층신경망)이 학습할 때 주어진 데이터를 학습하고 에러가 발생한 데이터에 기반하여 Weight(가중치) 값을 기존에서 새로운 W값으로 업데이트 시켜주면서 학습. input의 가중치합에 대해 임계값을 기준으로 두 가지 output 중 한 가지를 출력하는 구조.
# Perceptron(퍼셉트론, 단층신경망)이 학습할 때 주어진 데이터를 학습하고 에러가 발생한 데이터에 기반하여 Weight(가중치) 값을 기존에서
# 새로운 W값으로 업데이트 시켜주면서 학습. input의 가중치합에 대해 임계값을 기준으로 두 가지 output 중 한 가지를 출력하는 구조
# 논리회로로 실습
import numpy as np
from sklearn.linear_model import Perceptron
from sklearn.metrics import accuracy_score
feature = np.array([[0, 0],[0, 1],[1, 0],[1, 1]])
print(feature)
# label = np.array([0, 0, 0, 1]) # and
# label = np.array([0, 1, 1, 1]) # or
label = np.array([0, 1, 1, 0]) # xor, 세포체(Node)가 하나인 경우는 예측이 잘 안 된다.
ml = Perceptron(max_iter=10, eta0=0.1, verbose=1).fit(feature, label) # max_iter=학습 수, eta0=학습률
print(ml)
pred = ml.predict(feature)
print('pred :', pred)
print('acc :', accuracy_score(label, pred)) # 학습 수와 학습률에 따라 acc가 달라진다.
<console>
[[0 0]
[0 1]
[1 0]
[1 1]]
-- Epoch 1
Norm: 0.00, NNZs: 0, Bias: 0.000000, T: 4, Avg. loss: 0.100000
Total training time: 0.00 seconds.
-- Epoch 2
Norm: 0.00, NNZs: 0, Bias: 0.000000, T: 8, Avg. loss: 0.100000
Total training time: 0.00 seconds.
-- Epoch 3
Norm: 0.00, NNZs: 0, Bias: 0.000000, T: 12, Avg. loss: 0.100000
Total training time: 0.00 seconds.
-- Epoch 4
Norm: 0.00, NNZs: 0, Bias: 0.000000, T: 16, Avg. loss: 0.100000
Total training time: 0.00 seconds.
-- Epoch 5
Norm: 0.00, NNZs: 0, Bias: 0.000000, T: 20, Avg. loss: 0.100000
Total training time: 0.00 seconds.
-- Epoch 6
Norm: 0.00, NNZs: 0, Bias: 0.000000, T: 24, Avg. loss: 0.100000
Total training time: 0.00 seconds.
Convergence after 6 epochs took 0.00 seconds
Perceptron(eta0=0.1, verbose=1)
pred : [0 0 0 0]
acc : 0.5
단일신경망인 경우에는 xor 값을 잘 예측하지 못한다.
max_iter=학습 수, eta0=학습률