๋”ฅ๋Ÿฌ๋‹/Today I learned :

[๋”ฅ๋Ÿฌ๋‹] ์™€์ธ์˜ ์ข…๋ฅ˜ ์˜ˆ์ธกํ•˜๊ธฐ

์ฃผ์˜ ๐Ÿฑ 2021. 3. 25. 15:40
728x90
๋ฐ˜์‘ํ˜•

df_pre๋ผ๋Š” ๊ณต๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.

sample() ํ•จ์ˆ˜ : ์›๋ณธ ๋ฐ์ดํ„ฐ์˜ ๋ช‡ %๋ฅผ ์‚ฌ์šฉํ• ์ง€๋ฅผ ์ง€์ •, ์›๋ณธ ๋ฐ์ดํ„ฐ์—์„œ ์ •ํ•ด์ง„ ๋น„์œจ๋งŒํผ ๋žœ๋ค์œผ๋กœ ๋ฝ‘์•„์˜ค๋Š” ํ•จ์ˆ˜

frac = 1 : ์›๋ณธ ๋ฐ์ดํ„ฐ์˜ 100%๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋ผ๋Š” ์˜๋ฏธ (frac = 0.5๋กœ ์ง€์ •ํ•˜๋ฉด 50%๋งŒ ๋žœ๋ค)

df_pre = pd.read_csv(’../dataset/wine.csv’, header=None)
df = df_pre.sample(frac=1)

print(df.info())

Data

columns (total 13 columns):

0

6497

non-null

float64

1

6497

non-null

float64

2

6497

non-null

float64

3

6497

non-null

float64

4

6497

non-null

float64

5

6497

non-null

float64

6

6497

non-null

float64

7

6497

non-null

float64

8

6497

non-null

float64

9

6497

non-null

float64

10

6497

non-null

float64

11

6497

non-null

int64

12

6497

non-null

int64

dtypes: float64(11), int64(2)

memory usage: 710.6 KB

์ด 6497๊ฐœ์˜ ์ƒ˜ํ”Œ,

 13๊ฐœ์˜ ์†์„ฑ

0

์ฃผ์„์‚ฐ ๋†๋„

7

๋ฐ€๋„

1

์•„์„ธํŠธ์‚ฐ ๋†๋„

8

pH

2

๊ตฌ์—ฐ์‚ฐ ๋†๋„

9

ํ™ฉ์‚ฐ์นผ๋ฅจ ๋†๋„

3

์ž”๋ฅ˜ ๋‹น๋ถ„ ๋†๋„

10

์•Œ์ฝ”์˜ฌ ๋„์ˆ˜

4

์—ผํ™”๋‚˜ํŠธ๋ฅจ ๋†๋„

11

์™€์ธ์˜ ๋ง›(0~10๋“ฑ๊ธ‰)

5

์œ ๋ฆฌ ์•„ํ™ฉ์‚ฐ ๋†๋„

12

class (1: ๋ ˆ๋“œ์™€์ธ, 0: ํ™”์ดํŠธ์™€์ธ)

6

์ด ์•„ํ™ฉ์‚ฐ ๋†๋„

 

 

0~11๊นŒ์ง€์— ํ•ด๋‹นํ•˜๋Š” 12๊ฐœ์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  13๋ฒˆ์งธ ํด๋ž˜์Šค๋ฅผ ๋งž์ถ”๋Š” ๊ณผ์ œ

 

#X,Y๊ฐ’ ์„ค์ •
dataset = df.values
X = dataset[:,0:12]
Y = dataset[:,12]

 


<๋”ฅ๋Ÿฌ๋‹ ์‹คํ–‰>

  1. 4๊ฐœ์˜ ์€๋‹‰์ธต์„ ๋งŒ๋“ค์–ด ๊ฐ๊ฐ 30, 12, 8, 1๊ฐœ์˜ ๋…ธ๋“œ ๋ถ€์—ฌ
  2. ์ดํ•ญ ๋ถ„๋ฅ˜(binary classification) ๋ฌธ์ œ์ด๋ฏ€๋กœ ์˜ค์ฐจ ํ•จ์ˆ˜๋Š” binary_crossentropy๋ฅผ ์‚ฌ์šฉ, ์ตœ์ ํ™” ํ•จ์ˆ˜๋กœ adam()์„ ์‚ฌ์šฉ
  3. ์ „์ฒด ์ƒ˜ํ”Œ์ด 200ํšŒ ๋ฐ˜๋ณต๋˜์–ด ์ž…๋ ฅ๋  ๋•Œ๊นŒ์ง€ ์‹คํ—˜ ๋ฐ˜๋ณต.
  4. ํ•œ ๋ฒˆ์— ์ž…๋ ฅ๋˜๋Š” ์ž…๋ ฅ ๊ฐ’์€ 200๊ฐœ์”ฉ ๋˜๊ฒŒ๋” ํ•˜์—ฌ ์ข…ํ•ฉ
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import ModelCheckpoint, EarlyStopping

import pandas as pd
import numpy
import tensorflow as tf
import matplotlib.pyplot as plt

# seed ๊ฐ’ ์„ค์ •
seed = 0
numpy.random.seed(seed)
tf.random.set_seed(3)

# ๋ฐ์ดํ„ฐ ์ž…๋ ฅ
df_pre = pd.read_csv('../dataset/wine.csv', header=None)
df = df_pre.sample(frac=1)

dataset = df.values
X = dataset[:,0:12]
Y = dataset[:,12]

# ๋ชจ๋ธ ์„ค์ •
model = Sequential()
model.add(Dense(30,  input_dim=12, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

#๋ชจ๋ธ ์ปดํŒŒ์ผ
model.compile(loss='binary_crossentropy',
           optimizer='adam',
           metrics=['accuracy'])

# ๋ชจ๋ธ ์‹คํ–‰
model.fit(X, Y, epochs=200, batch_size=200)

# ๊ฒฐ๊ณผ ์ถœ๋ ฅ
print("\n Accuracy: %.4f" % (model.evaluate(X, Y)[1]))

Accuracy: 0.9858

์ •ํ™•๋„ : 98.58%

 


๋ชจ๋ธ ์—…๋ฐ์ดํŠธ

 

์—ํฌํฌ(epoch)๋งˆ๋‹ค ๋ชจ๋ธ์˜ ์ •ํ™•๋„๋ฅผ ํ•จ๊ป˜ ๊ธฐ๋กํ•˜๋ฉด์„œ ์ €์žฅ

 

  1. ๋จผ์ € ๋ชจ๋ธ์ด ์ €์žฅ๋  ํด๋” ์ง€์ •.(ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์— model ํด๋”) 
  2. ํ•ด๋‹น ํด๋”๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์—†์œผ๋ฉด ํด๋” ์ƒ์„ฑ.
  3. ์—ํฌํฌ ํšŸ์ˆ˜์™€ ์ด๋•Œ์˜ ํ…Œ์ŠคํŠธ์…‹ ์˜ค์ฐจ ๊ฐ’์„ ์ด์šฉํ•ด ํŒŒ์ผ ์ด๋ฆ„์„ ๋งŒ๋“ค์–ด hdf 5๋ผ๋Š” ํ™•์žฅ์ž๋กœ ์ €์žฅ (์˜ˆ๋ฅผ ๋“ค์–ด, 100๋ฒˆ์งธ ์—ํฌํฌ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๋‚œ ๊ฒฐ๊ณผ ์˜ค์ฐจ๊ฐ€ 0.0612๋ผ๋ฉด, ํŒŒ์ผ๋ช…์€ 100-0.0612.hdf 5)
import os

MODEL_DIR = ’./model/’             # ๋ชจ๋ธ์„ ์ €์žฅํ•˜๋Š” ํด๋”
if not os.path.exists(MODEL_DIR):  # ๋งŒ์ผ ์œ„์˜ ํด๋”๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด
    os.mkdir(MODEL_DIR)            # ์ด ์ด๋ฆ„์˜ ํด๋”๋ฅผ ๋งŒ๋“ค์–ด ์คŒ

modelpath=”./model/{epoch:02d}-{val_loss:.4f}.hdf5”

 

๋ชจ๋ธ ์ €์žฅ -์ผ€๋ผ์Šค์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์ค‘ ModelCheckpoint() ํ•จ์ˆ˜ 

 

checkpointer๋ผ๋Š” ๋ณ€์ˆ˜์— ๋ชจ๋‹ˆํ„ฐํ•  ๊ฐ’์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

ํ…Œ์ŠคํŠธ ์˜ค์ฐจ๋Š” ์ผ€๋ผ์Šค ๋‚ด๋ถ€์—์„œ val_loss๋กœ ๊ธฐ๋ก(์ฐธ๊ณ ๋กœ ํ•™์Šต ์ •ํ™•๋„๋Š” acc, ํ…Œ์ŠคํŠธ์…‹ ์ •ํ™•๋„๋Š” val_acc, ํ•™์Šต์…‹ ์˜ค์ฐจ๋Š” loss๋กœ ๊ฐ๊ฐ ๊ธฐ๋ก).

๋ชจ๋ธ์ด ์ €์žฅ๋  ๊ณณ์„ ์•ž์„œ ๋งŒ๋“  modelpath๋กœ ์ง€์ •ํ•˜๊ณ  verbose์˜ ๊ฐ’์„ 1๋กœ ์ •ํ•˜๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜์˜ ์ง„ํ–‰ ์‚ฌํ•ญ์ด ์ถœ๋ ฅ๋˜๊ณ , 0์œผ๋กœ ์ •ํ•˜๋ฉด ์ถœ๋ ฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

from keras.callbacks import ModelCheckpoint

checkpointer = ModelCheckpoint(filepath=modelpath, monitor=‘val_loss’, verbose=1,save_best_only=True)

model.fit(X, Y, validation_split=0.2, epochs=200, batch_size=200, verbose=0, callbacks=[checkpointer])
์‹คํ–‰๊ฒฐ๊ณผ
Epoch 00194: saving model to ./model/194-0.0629.hdf5 Epoch 00195: saving model to ./model/195-0.0636.hdf5 Epoch 00196: saving model to ./model/196-0.0630.hdf5 Epoch 00197: saving model to ./model/197-0.0695.hdf5 Epoch 00198: saving model to ./model/198-0.0724.hdf5 Epoch 00199: saving model to ./model/199-0.0635.hdf5

Epoch 0~199๊นŒ์ง€ ์ด 200๊ฐœ์˜ ๋ชจ๋ธ์ด model ํด๋”์— ์ €์žฅ.

์ €์žฅ๋œ ํŒŒ์ผ์˜ ์ด๋ฆ„ = ์—ํฌํฌ ์ˆ˜์™€ ์ด๋•Œ์˜ ํ…Œ์ŠคํŠธ์…‹ ์˜ค์ฐจ ๊ฐ’

 

ModelCheckpoint() ํ•จ์ˆ˜์— ๋ชจ๋ธ์ด ์•ž์„œ ์ €์žฅํ•œ ๋ชจ๋ธ๋ณด๋‹ค ๋‚˜์•„์กŒ์„ ๋•Œ๋งŒ ์ €์žฅํ•˜๊ฒŒ๋” ํ•˜๋ ค๋ฉด save_best_only ๊ฐ’ = True

 

 

from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import ModelCheckpoint

import pandas as pd
import numpy
import os
import tensorflow as tf

# seed ๊ฐ’ ์„ค์ •
numpy.random.seed(3)
tf.random.set_seed(3)

df_pre = pd.read_csv('../dataset/wine.csv', header=None)
df = df_pre.sample(frac=1)

dataset = df.values
X = dataset[:,0:12]
Y = dataset[:,12]

# ๋ชจ๋ธ์˜ ์„ค์ •
model = Sequential()
model.add(Dense(30,  input_dim=12, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# ๋ชจ๋ธ ์ปดํŒŒ์ผ
model.compile(loss='binary_crossentropy',
          optimizer='adam',
          metrics=['accuracy'])

# ๋ชจ๋ธ ์ €์žฅ ํด๋” ์„ค์ •
MODEL_DIR = './model/'
if not os.path.exists(MODEL_DIR):
   os.mkdir(MODEL_DIR)

# ๋ชจ๋ธ ์ €์žฅ ์กฐ๊ฑด ์„ค์ •
modelpath="./model/{epoch:02d}-{val_loss:.4f}.hdf5"
checkpointer = ModelCheckpoint(filepath=modelpath, monitor='val_loss', verbose=1, save_best_only=True)

# ๋ชจ๋ธ ์‹คํ–‰ ๋ฐ ์ €์žฅ
model.fit(X, Y, validation_split=0.2, epochs=200, batch_size=200, verbose=0, callbacks=[checkpointer])

 


ํ•™์Šต์‹œ๊ฐ„์— ๋”ฐ๋ฅธ ์ •ํ™•๋„์™€ ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ํ‘œํ˜„ํ•˜๊ธฐ

 

df = df_pre.sample(frac=0.15)
history = model.fit(X, Y, validation_split=0.33, epochs=3500, batch_size=500)

history =๋ชจ๋ธ์ด ํ•™์Šต๋˜๋Š” ๊ณผ์ • ์ €์žฅ

๊ธด ํ•™์Šต์˜ ์˜ˆ๋ฅผ ์ง€์ผœ ๋ณด๊ธฐ ์œ„ํ•ด ์—ํฌํฌ(epoch)๋ฅผ 3,500์œผ๋กœ ์กฐ์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ์ง€ ์•Š๋„๋ก sample() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ 15%๋งŒ ๋ถˆ๋Ÿฌ์˜ค๊ฒŒ ํ•˜๊ณ , ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 500์œผ๋กœ ๋Š˜๋ ค ํ•œ ๋ฒˆ ๋”ฅ๋Ÿฌ๋‹์„ ๊ฐ€๋™ํ•  ๋•Œ ๋” ๋งŽ์ด ์ž…๋ ฅ๋˜๊ฒŒ๋” ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ถˆ๋Ÿฌ์˜จ ์ƒ˜ํ”Œ ์ค‘ 33%๋Š” ๋ถ„๋ฆฌํ•˜์—ฌ ํ…Œ์ŠคํŠธ์…‹์œผ๋กœ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ž˜ํ”„๋กœ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ์˜ค์ฐจ์™€ ์ •ํ™•๋„์˜ ๊ฐ’์„ ์ •ํ•ฉ๋‹ˆ๋‹ค. y_vloss์— ํ…Œ์ŠคํŠธ์…‹(33%)์œผ๋กœ ์‹คํ—˜ํ•œ ๊ฒฐ๊ณผ์˜ ์˜ค์ฐจ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ , y_acc์— ํ•™์Šต์…‹(67%)์œผ๋กœ ์ธก์ •ํ•œ ์ •ํ™•๋„์˜ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

import matplotlib.pyplot as plt
 
y_vloss=history.history['val_loss']
y_acc=history.history['acc']

x ๊ฐ’์„ ์ง€์ •ํ•˜๊ณ  ์ •ํ™•๋„๋ฅผ ํŒŒ๋ž€์ƒ‰์œผ๋กœ, ์˜ค์ฐจ๋ฅผ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ํ‘œ์‹œ

 

x_len = numpy.arange(len(y_acc))
plt.plot(x_len, y_vloss, "o", c="red", markersize=3)
plt.plot(x_len, y_acc, "o", c="blue", markersize=3)

 

์ „์ฒด ์ฝ”๋“œ

from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import ModelCheckpoint

import pandas as pd
import numpy
import os
import matplotlib.pyplot as plt
import tensorflow as tf

# seed ๊ฐ’ ์„ค์ •
numpy.random.seed(3)
tf.random.set_seed(3)

df_pre = pd.read_csv('../dataset/wine.csv', header=None)
df = df_pre.sample(frac=0.15)

dataset = df.values
X = dataset[:,0:12]
Y = dataset[:,12]

# ๋ชจ๋ธ์˜ ์„ค์ •
model = Sequential()
model.add(Dense(30,  input_dim=12, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# ๋ชจ๋ธ ์ปดํŒŒ์ผ
model.compile(loss='binary_crossentropy',
          optimizer='adam',
          metrics=['accuracy'])

# ๋ชจ๋ธ ์ €์žฅ ํด๋” ์„ค์ •
MODEL_DIR = './model/'
if not os.path.exists(MODEL_DIR):
   os.mkdir(MODEL_DIR)

# ๋ชจ๋ธ ์ €์žฅ ์กฐ๊ฑด ์„ค์ •
modelpath="./model/{epoch:02d}-{val_loss:.4f}.hdf5"
checkpointer = ModelCheckpoint(filepath=modelpath, monitor='val_loss', verbose=1, save_best_only=True)

# ๋ชจ๋ธ ์‹คํ–‰ ๋ฐ ์ €์žฅ
history = model.fit(X, Y, validation_split=0.33, epochs=3500, batch_size=500)

# y_vloss์— ํ…Œ์ŠคํŠธ์…‹์œผ๋กœ ์‹คํ—˜ ๊ฒฐ๊ณผ์˜ ์˜ค์ฐจ ๊ฐ’์„ ์ €์žฅ
y_vloss=history.history['val_loss']

# y_acc ์— ํ•™์Šต ์…‹์œผ๋กœ ์ธก์ •ํ•œ ์ •ํ™•๋„์˜ ๊ฐ’์„ ์ €์žฅ
y_acc=history.history['acc']

# x๊ฐ’์„ ์ง€์ •ํ•˜๊ณ  ์ •ํ™•๋„๋ฅผ ํŒŒ๋ž€์ƒ‰์œผ๋กœ, ์˜ค์ฐจ๋ฅผ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ํ‘œ์‹œ
x_len = numpy.arange(len(y_acc))
plt.plot(x_len, y_vloss, "o", c="red", markersize=3)
plt.plot(x_len, y_acc, "o", c="blue", markersize=3)

plt.show()

 

ํ•™์Šต์…‹์˜ ์ •ํ™•๋„๋Š” ์‹œ๊ฐ„์ด ํ๋ฅผ์ˆ˜๋ก ์ข‹์•„์ง€์ง€๋งŒ ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ๋Š” ์–ด๋Š ์ •๋„ ์ด์ƒ ์‹œ๊ฐ„์ด ํ๋ฅด๋ฉด ๋” ๋‚˜์•„์ง€์ง€ ์•Š๋Š”๋‹ค

 

 

 

 


ํ•™์Šต์˜ ์ž๋™ ์ค‘๋‹จ

ํ•™์Šต์ด ์ง„ํ–‰๋ ์ˆ˜๋ก ํ•™์Šต์…‹์˜ ์ •ํ™•๋„๋Š” ์˜ฌ๋ผ๊ฐ€์ง€๋งŒ ๊ณผ์ ํ•ฉ ๋•Œ๋ฌธ์— ํ…Œ์ŠคํŠธ์…‹์˜ ์‹คํ—˜ ๊ฒฐ๊ณผ๋Š” ์ ์  ๋‚˜๋น ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

 EarlyStopping() ํ•จ์ˆ˜ : ํ•™์Šต์ด ์ง„ํ–‰๋˜์–ด๋„ ํ…Œ์ŠคํŠธ์…‹ ์˜ค์ฐจ๊ฐ€ ์ค„์ง€ ์•Š์œผ๋ฉด ํ•™์Šต์„ ๋ฉˆ์ถ”๊ฒŒ ํ•˜๋Š” ํ•จ์ˆ˜

 

early_stopping_callback์— EarlyStopping() ํ•จ์ˆ˜์— ๋ชจ๋‹ˆํ„ฐํ•  ๊ฐ’๊ณผ ํ…Œ์ŠคํŠธ ์˜ค์ฐจ๊ฐ€ ์ข‹์•„์ง€์ง€ ์•Š์•„๋„ ๋ช‡ ๋ฒˆ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆด์ง€ ์ €์žฅ

from keras.callbacks import EarlyStopping

early_stopping_callback = EarlyStopping(monitor=‘val_loss’, patience=100)

์•ž์„œ ์ •ํ•œ ๊ทธ๋Œ€๋กœ ์—ํฌํฌ ํšŸ์ˆ˜์™€ ๋ฐฐ์น˜ ํฌ๊ธฐ ๋“ฑ์„ ์„ค์ •ํ•˜๊ณ  early_stopping_callback ๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.

model.fit(X, Y, validation_split=0.33, epochs=2000, batch_size=500, callbacks=[early_stopping_callback])

 

์ „์ฒด ์ฝ”๋“œ(์ž๋™ ์ค‘๋‹จ)

from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping

import pandas as pd
import numpy
import tensorflow as tf

# seed ๊ฐ’ ์„ค์ •
numpy.random.seed(3)
tf.random.set_seed(3)

df_pre = pd.read_csv('../dataset/wine.csv', header=None)
df = df_pre.sample(frac=0.15)

dataset = df.values
X = dataset[:,0:12]
Y = dataset[:,12]

model = Sequential()
model.add(Dense(30,  input_dim=12, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
           optimizer='adam',
           metrics=['accuracy'])

# ์ž๋™ ์ค‘๋‹จ ์„ค์ •
early_stopping_callback = EarlyStopping(monitor='val_loss', patience=100)

# ๋ชจ๋ธ ์‹คํ–‰
model.fit(X, Y, validation_split=0.2, epochs=2000, batch_size=500, callbacks=[early_stopping_callback])

# ๊ฒฐ๊ณผ ์ถœ๋ ฅ
print("\n Accuracy: %.4f" % (model.evaluate(X, Y)[1]))
์‹คํ–‰๊ฒฐ๊ณผ
Epoch 1531/2000 780/780 [==============================] - 0s 8us/step - loss: 0.0303 - accuracy: 0.9872 - val_loss: 0.0586 - val_accuracy: 0.9795
Epoch 1532/2000 780/780 [==============================] - 0s 6us/step - loss: 0.0308 - accuracy: 0.9859 - val_loss: 0.0627 - val_accuracy: 0.9795
Epoch 1533/2000 780/780 [==============================] - 0s 8us/step - loss: 0.0294 - accuracy: 0.9872 - val_loss: 0.0599 - val_accuracy: 0.9795 975/975 [==============================] - 0s 16us/step
Accuracy: 0.9877

์—ํฌํฌ๋ฅผ 2,000์œผ๋กœ ์„ค์ •ํ•˜์˜€์ง€๋งŒ, ๋„์ค‘์— ๊ณ„์‚ฐ์ด ๋ฉˆ์ถ”๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 


๋ชจ๋ธ ์—…๋ฐ์ดํŠธ ํ•จ์ˆ˜ + ํ•™์Šต ์ž๋™ ์ค‘๋‹จ ํ•จ์ˆ˜ ์ „์ฒด ์ฝ”๋“œ

 

from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import ModelCheckpoint,EarlyStopping

import pandas as pd
import numpy
import os
import tensorflow as tf

# seed ๊ฐ’ ์„ค์ •
numpy.random.seed(3)
tf.random.set_seed(3)

df_pre = pd.read_csv('../dataset/wine.csv', header=None)
df = df_pre.sample(frac=0.15)

dataset = df.values
X = dataset[:,0:12]
Y = dataset[:,12]

model = Sequential()
model.add(Dense(30,  input_dim=12, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
          optimizer='adam',
          metrics=['accuracy'])

# ๋ชจ๋ธ ์ €์žฅ ํด๋” ๋งŒ๋“ค๊ธฐ
MODEL_DIR = './model/'
if not os.path.exists(MODEL_DIR):
   os.mkdir(MODEL_DIR)

modelpath="./model/{epoch:02d}-{val_loss:.4f}.hdf5"

# ๋ชจ๋ธ ์—…๋ฐ์ดํŠธ ๋ฐ ์ €์žฅ
checkpointer = ModelCheckpoint(filepath=modelpath, monitor='val_loss', verbose=1, save_best_only=True)

# ํ•™์Šต ์ž๋™ ์ค‘๋‹จ ์„ค์ •
early_stopping_callback = EarlyStopping(monitor='val_loss', patience=100)

model.fit(X, Y, validation_split=0.2, epochs=3500, batch_size=500, verbose=0, callbacks=[early_stopping_callback,checkpointer])

Epoch 01344: val_loss did not improve from 0.05648

Epoch 01345: val_loss did not improve from 0.05648

Epoch 01346: val_loss improved from 0.05648 to 0.05631, saving model to ./model/1346-0.0563.hdf5

 

Epoch 01395: val_loss did not improve from 0.05631

Epoch 01396: val_loss did not improve from 0.05631

Epoch 01397: val_loss did not improve from 0.05631

Epoch 01398: val_loss improved from 0.05631 to 0.05524, saving model to ./model/1398-0.0552.hdf5

 

Epoch 01496: val_loss did not improve from 0.05524

Epoch 01497: val_loss did not improve from 0.05524

Epoch 01498: val_loss did not improve from 0.05524

๋ฐ˜์‘ํ˜•