ํ์ด์ฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ KERAS
- ์์ง์ผ๋ก๋ Tensorflow, Theano, CNTK ๋ฑ์ด ์์
- ์ผ๋ผ์ค๋ ์ธ๊ณต์ง๋ฅ ์์ง ํ๋ก๊ทธ๋จ์ ํธ์ถํ์ฌ ์ธ๊ณต์ง๋ฅ ์๊ณ ๋ฆฌ์ฆ์ ์ํํ๋ค
- ์ผ๋ผ์ค๋ ํน์ ์์ง์ ๊ตญํ๋์ง ์๋ ๊ณตํต ๋ฐฑ์๋ ํจ์๋ ์ ๊ณตํด์ค๋ค.
models
๋ ์ธ๊ณต์ ๊ฒฝ๋ง์ ๊ฐ ๊ฒ์ธต์ ์ฐ๊ฒฐํ์ฌ ํ๋์ ๋ชจ๋ธ์ ๋ง๋ ํ ์ปดํ์ผ, ํ์ต, ์์ธก์ ๋ด๋น
layers
๋ ์ธ๊ณต์ ๊ฒฝ๋ง์ ๊ฐ ๊ณ์ธต์ ๋ง๋๋ ํด๋์ค ์ ๊ณต
# ์ผ๋ผ์ค๋ก ์ธ๊ณต ์ ๊ฒฝ๋ง ๋ชจ๋ธ ๋ง๋ฆ. models.Sequential()์ ์ฌ์ฉํ์ฌ ํ์ด์ฌ ํ๋ก์ธ์ค์๊ฒ ์๋ฆผ
## models.Sequential์ ํ์ด์ฌ์ ํด๋์ค
## model์ด๋ผ๋ ์ธ์คํด์ค ๋ง๋ฆ
model = keras.models.Sequential()
# ๋ชจ๋ธ ์ธ์คํด์ค๊ฐ ์์ฑ๋๋ฉด ๋ฉค๋ฒ ํจ์ add()๋ฅผ ์ด์ฉํ์ฌ ์ธ๊ณต์ง๋ฅ ๊ณ์ธต ์ถ๊ฐ
model.add(keras.layers.Dense(1, input_shape =(1,))
# ํ์ต์ ์ฌ์ฉ๋๋ ์ต์ ํ ์๊ณ ๋ฆฌ์ฆ -> ํ๋ฅ ์ ๊ฒฝ์ฌํ๊ฐ๋ฒ, ์์คํจ์ -> ํ๊ท ์ ๊ณฑ ์ค์ฐจ
model.compile('SGD', 'mse')
# ๋ชจ๋ธ์ ์ฃผ์ด์ง ๋ฐ์ดํฐ๋ก ํ์ต
## verbose๋ ํ์ต ์งํ์ฌํญ ํ์ ์ฌ๋ถ
model.fit(x[:2], y[:2], epochs = 1000, verbose = 0)
# ์ฑ๋ฅ ํ๊ฐ
print("Targets:", y[2:])
print("Predictions:", model.predict(x[2:]).flatten())
class ANN(models.Model):
def __init__(self, Nin, Nh, Nout):
# Prepare network layers and activate functions
hidden = layers.Dense(Nh)
output = layers.Dense(Nout)
relu = layers.Activation('relu')
softmax = layers.Activation('softmax')
# Connect network elements
x = layers.Input(shape = (Nin,))
h = relu(hidden(x))
y = softmax(output(h))
super().__init__(x, y)
self.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
class ANN(models.Sequential):
def __init__(self, Nin, Nh, Nout):
super().__init__()
self.add(layers.Dense(Nh, activation = 'relu', input_shape = (Nin,)))
self.add(layers.Dense(Nout, activation = 'softmax'))
self.compil (loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
def ANN(Nin, Nh, Nout):
x = layers.Input(shape = (Nin,))
h = layers.Activation('relu')(layers.Dense(Nh)(x))
...
model = models.Model(x,y)
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy']
return model
def ANN(Nin, Nh, Nout):
model = models.Sequential()
model.add(layers.Dense(Nh, activation = 'relu', input_shape = (Nin,)))
model.add(layers.Dense(Nout, avtivation = 'softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
return model