๐Ÿง  ์‹ ๊ฒฝ๋ง ๐Ÿง 

parkeuยท2022๋…„ 9์›” 27์ผ
0

ABC๋ถ€ํŠธ์บ ํ”„

๋ชฉ๋ก ๋ณด๊ธฐ
33/55

๐Ÿคฏ ์‹ ๊ฒฝ๋ง

์‹ ๊ฒฝ๋ง ์ž‘๋™์›๋ฆฌ

  • ์‹ ๊ฒฝ๋ง์€ ๊ฐ€์ค‘์น˜๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๊ฐ€์ง„๋‹ค.
  • ์ค‘์š”ํ•œ feature ๊ฐ€์ค‘์น˜ ๋†’๊ฒŒ
  • ์†์‹ค ํ•จ์ˆ˜๊ฐ€ ์‹ ๊ฒฝ๋ง์˜ ์ถœ๋ ฅ ํ’ˆ์งˆ์„ ์ธก์ •
  • ์†์‹ค์ ์ˆ˜๊ฐ€ ๋‚ฎ๋„๋ก ๊ฐ€์ค‘์น˜ ์กฐ์ •
  • ์†์‹ค ์ ์ˆ˜๋ฅผ ํ”ผ๋“œ๋ฐฑ ์‹ ํ˜ธ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ค‘์น˜ ์กฐ์ ˆ
  • ๊ธฐ๋ณธ์ ์ธ ๋”ฅ๋Ÿฌ๋‹ ๋ฐฉ์‹ : ์†์‹ค์ ์ˆ˜๋ฅผ ํ”ผ๋“œ๋ฐฑ ์‹ ํ˜ธ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ์ƒ˜ํ”Œ์˜ ์†์‹ค ์ ์ˆ˜๊ฐ€ ๊ฐ์†Œ๋˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€์ค‘์น˜๋ฅผ ์กฐ๊ธˆ์”ฉ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ

๐Ÿงฌ ํผ์…‰ํŠธ๋ก 

  • ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์‹ ํ˜ธ๋ฅผ ์ž…๋ ฅ์„ ๋ฐ›์•„ ํ•˜๋‚˜์˜ ๊ฐ’์„ ์ถœ๋ ฅ
  • x : ์ž…๋ ฅ, y : ์ถœ๋ ฅ, w : ๊ฐ€์ค‘์น˜
  • x์™€ ๊ฐ€์ค‘์น˜ w๋ฅผ ๊ณฑํ•œ ๊ฐ’์„ ๋ชจ๋‘ ๋”ํ•˜์—ฌ ํ•˜๋‚˜์˜ ๊ฐ’y์œผ๋กœ ๋งŒ๋“ค์–ด๋ƒ„
  • ์ด๋•Œ ์ž„๊ณ„๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ ํฌ๋ฉด 1, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 0์„ ์ถœ๋ ฅ
    -> ํ™œ์„ฑํ™” ํ•จ์ˆ˜(Activation Function)
    -> ์œ„์—์„œ ์‚ฌ์šฉํ•œ ๊ฒƒ์€ ๊ณ„๋‹จ ํ•จ์ˆ˜(Step Function)

    ์ž…๋ ฅ์ด ๋‘๊ฐœ์ธ ํผ์…‰ํŠธ๋ก 

OR ๊ฒŒ์ดํŠธ ๊ตฌํ˜„ํ•˜๊ธฐ

OR๊ฒŒ์ดํŠธ : 1์ด ํ•˜๋‚˜๋ผ๋„ ์žˆ์œผ๋ฉด 1

x1x2y
000
101
011
111

๐Ÿผ ๋ฌธ์ œ์ •์˜

๋ฌธ์ œ์ •์˜ : OR ๊ฒŒ์ดํŠธ ๊ตฌํ˜„

  • 0,0 -> 0
  • 1,0 -> 1
  • 0,1 -> 1
  • 1,1 -> 1

๋ฐ์ดํ„ฐ ์ค€๋น„ํ•˜๊ธฐ

import tensorflow as tf
tf.random.set_seed(777)

import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.losses import mse

# ๋ฐ์ดํ„ฐ ์ค€๋น„ํ•˜๊ธฐ
X = np.array([[0,0],[1,0],[0,1],[1,1]])
y = np.array([[0], [1], [1], [1]])

๋ชจ๋ธ ๊ตฌ์„ฑ, ์„ค์ •, ํ•™์Šตํ•˜๊ธฐ

# ๋ชจ๋ธ ๊ตฌ์„ฑํ•˜๊ธฐ
model = Sequential()
# ํ•œ๊ฐœ์˜ layer์— ํ•œ๊ฐœ์˜ Dense์ธต
model.add(Dense(1,input_shape=(2,), activation='linear')) # ๋‹จ์ธต ํผ์…‰ํŠธ๋ก 

# ๋ชจ๋ธ ์„ค์ •ํ•˜๊ธฐ
model.compile(optimizer=SGD(), loss=mse, metrics=['acc']) # ์†์‹คํ•จ์ˆ˜ : mse, ์ •ํ™•๋„ acc๋กœ ํ™•์ธ

# ๋ชจ๋ธ ํ•™์Šตํ•˜๊ธฐ
# (ํ•™์Šต๋ฐ์ดํ„ฐ, ์ •๋‹ต, 500๋ฒˆ ๊ณต๋ถ€)
history = model.fit(X, y, epochs=500) 

ํ›ˆ๋ จ ๋ฐ ๊ฒ€์ฆ -> ์†์‹ค, ์ •ํ™•๋„

import matplotlib.pyplot as plt

his_dict = history.history
loss = his_dict['loss']

epochs = range(1, len(loss) + 1)
fig = plt.figure(figsize = (10, 5))

# ํ›ˆ๋ จ ๋ฐ ๊ฒ€์ฆ ์†์‹ค ๊ทธ๋ฆฌ๊ธฐ
ax1 = fig.add_subplot(1, 2, 1)
ax1.plot(epochs, loss, color = 'orange', label = 'train_loss')
ax1.set_title('train loss')
ax1.set_xlabel('epochs')
ax1.set_ylabel('loss')
ax1.legend()

acc = his_dict['acc']

# ํ›ˆ๋ จ ๋ฐ ๊ฒ€์ฆ ์ •ํ™•๋„ ๊ทธ๋ฆฌ๊ธฐ
ax2 = fig.add_subplot(1, 2, 2)
ax2.plot(epochs, acc, color = 'blue', label = 'train_accuracy')
ax2.set_title('train accuracy')
ax2.set_xlabel('epochs')
ax2.set_ylabel('accuracy')
ax2.legend()

plt.show()


XOR ๊ฒŒ์ดํŠธ ๊ตฌํ˜„ํ•˜๊ธฐ

  • ๊ฐ™์œผ๋ฉด 0, ๋‹ค๋ฅด๋ฉด 1
  • OR ๊ฒŒ์ดํŠธ ์ฝ”๋“œ ์ค‘ ๋ฐ์ดํ„ฐ ์ค€๋น„ํ•˜๊ธฐ ์ฝ”๋“œ๋งŒ ๋ณ€๊ฒฝ
# ๋ฐ์ดํ„ฐ ์ค€๋น„ํ•˜๊ธฐ
# X1, X2๊ฐ€ ๋‹ค๋ฅผ ๋•Œ 1, ๊ฐ™์„ ๋•Œ 0
X = np.array([[0,0],[1,0],[0,1],[1,1]]) 
y = np.array([[0], [1], [1], [0]])


๐Ÿ‘€ OR ๊ฒŒ์ดํŠธ์™€ ๋‹ฌ๋ฆฌ accuracy๊ฐ€ ๋†’์ง€ ์•Š์Œ -> ๋‹ค๋ฅธ ๋ฐฉ๋ฒ• ํ•„์š”!


๐Ÿงฌ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก 

XOR ๊ฒŒ์ดํŠธ ์ธต ์ถ”๊ฐ€

# ๋ฐ์ดํ„ฐ ์ค€๋น„ํ•˜๊ธฐ
# X1, X2๊ฐ€ ๋‹ค๋ฅผ ๋•Œ 1, ๊ฐ™์„ ๋•Œ 0
X = np.array([[0,0],[1,0],[0,1],[1,1]]) 
y = np.array([[0], [1], [1], [0]])

model = Sequential()
# ํ•œ๊ฐœ์˜ layer์— ํ•œ๊ฐœ์˜ Dense์ธต
model.add(Dense(32,input_shape=(2,), activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer=SGD(), loss=mse, metrics=['acc']) # ์†์‹คํ•จ์ˆ˜ : mse, ์ •ํ™•๋„ acc๋กœ ํ™•์ธ

# (ํ•™์Šต๋ฐ์ดํ„ฐ, ์ •๋‹ต, 500๋ฒˆ ๊ณต๋ถ€)
history = model.fit(X, y, epochs=500) 


๐Ÿ‘€ ์—ฌ์ „ํžˆ accuracy๊ฐ€ ๋‚ฎ์Œ


XOR ๊ฒŒ์ดํŠธ ์ธต ์ถ”๊ฐ€ + ์˜ตํ‹ฐ๋งˆ์ด์ € ๋ณ€๊ฒฝ

from tensorflow.keras.optimizers import RMSprop
# ๋ฐ์ดํ„ฐ ์ค€๋น„ํ•˜๊ธฐ
# X1, X2๊ฐ€ ๋‹ค๋ฅผ ๋•Œ 1, ๊ฐ™์„ ๋•Œ 0
X = np.array([[0,0],[1,0],[0,1],[1,1]]) 
y = np.array([[0], [1], [1], [0]])

xor_model = Sequential()
# ํ•œ๊ฐœ์˜ layer์— ํ•œ๊ฐœ์˜ Dense์ธต
xor_model.add(Dense(32,input_shape=(2,), activation='relu'))
xor_model.add(Dense(1, activation='sigmoid'))

xor_model.compile(optimizer=RMSprop(), loss=mse, metrics=['acc']) # ์†์‹คํ•จ์ˆ˜ : mse, ์ •ํ™•๋„ acc๋กœ ํ™•์ธ

# (ํ•™์Šต๋ฐ์ดํ„ฐ, ์ •๋‹ต, 100๋ฒˆ ๊ณต๋ถ€)
history = xor_model.fit(X, y, epochs=100) 


๐Ÿ‘€ accuracy๊ฐ€ epoch๋ฅผ ์ค„์˜€์Œ์—๋„ ์˜ฌ๋ผ๊ฐ


ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ํŠœ๋‹

profile
๋ฐฐ๊ณ ํŒŒ์šฉ.

0๊ฐœ์˜ ๋Œ“๊ธ€