퍼셉트론은 두 개의 노드가 있을 경우, 그 두 개의 노드가 각각 들어가야 하는 위치인 입력치와 그를 가중하는 가중치, 이를 통해 계산하여 나온 결과인 출력 값으로 구성되어 있다.
가중치와 입력치를 곱한 것을 모두 합한 값이 활성함수에 의해 판단되는데, 그 값이 임계치(보통 0)보다 크면 뉴런(혹은 내부 함수)이 활성화되고 결과값으로 1을 출력한다. 이를 통해 값이 0 또는 1로 결정된다. 이렇게 나온 값을 출력 값이라고 하며, 이 과정을 통틀어 퍼셉트론
텐서플로를 활용해서 perceptron 예제를 만들어보려고 한다.!
import tensorflow as tf
import numpy as np
텐서플로우와 numpy를 import 해준다.
이번엔 Iris 데이터 중 두 종류를 분류하는 퍼셉트론을 제작한다. y값은 1 또는 -1을 사용하고 활성화 함수로는 하이퍼탄젠트(hypertangent)함수를 사용한다.
비용 함수로는 다음 식을 사용한다.
from sklearn.datasets import load_iris
iris = load_iris()
print(iris.DESCR)
머신러닝을 해야하기 때문에 sklearn을 불러와주고 진행한다.
idx = np.in1d(iris.target, [0, 2])
X_data = iris.data[idx, 0:2]
y_data = (iris.target[idx] - 1.0)[:, np.newaxis]
X_data.shape, y_data.shape
((100, 2), (100, 1))
다음과 같은 결과를 얻을 수 있다.
num_iter = 500
lr = 0.0003
w = tf.Variable(tf.random.normal([2, 1], dtype=tf.float64))
b = tf.Variable(tf.random.normal([1, 1], dtype=tf.float64))
zero = tf.constant(0, dtype=tf.float64)
for i in range(num_iter):
for i in range(X_data.shape[0]):
x = X_data[i:i+1]
y = y_data[i:i+1]
with tf.GradientTape() as tape:
logit = tf.matmul(x, w) + b
y_hat = tf.tanh(logit)
loss = tf.maximum(zero, tf.multiply(-y, y_hat))
grad = tape.gradient(loss, [w, b])
w.assign_add(-lr * grad[0])
b.assign_add(-lr * grad[1])
y_pred = tf.tanh(tf.matmul(X_data, w) + b)
print("예측치 :", y_pred[0].numpy(), "정답 :", y_data[0])
print("예측치 :", y_pred[51].numpy(), "정답 :", y_data[51])
print("예측치 :", y_pred[88].numpy(), "정답 :", y_data[88])
예측치 : [-0.09367716] 정답 : [-1.]
예측치 : [0.0916346] 정답 : [1.]
예측치 : [0.10907913] 정답 : [1.]
이렇게 예측치를 확인할 수 있다.