LEE_BOMB 2021. 12. 22. 18:42
keras binary

- Keras model : ์ดํ•ญ๋ถ„๋ฅ˜๊ธฐ 
X๋ณ€์ˆ˜ : ์ •๊ทœํ™”(0~1)
Y๋ณ€์ˆ˜ : one hot encoding(2์ง„์ˆ˜)

from sklearn.datasets import load_iris #dataset 
from sklearn.model_selection import train_test_split #split 
from sklearn.preprocessing import minmax_scale #X๋ณ€์ˆ˜ : ์ •๊ทœํ™”(0~1)

from tensorflow.keras.utils import to_categorical #Y๋ณ€์ˆ˜ : encoding 
from tensorflow.keras import Sequential #keras model ์ƒ์„ฑ 
from tensorflow.keras.layers import Dense #DNN layer ๊ตฌ์ถ•


1. dataset load & ์ „์ฒ˜๋ฆฌ 

X, y = load_iris(return_X_y=True)
X.shape #(150, 4) : ์ž…๋ ฅ์ˆ˜=4


X๋ณ€์ˆ˜ : ์ •๊ทœํ™” 

X = minmax_scale(X[:100])
print(X)


Y๋ณ€์ˆ˜ : one hot encoding

y_one = to_categorical(y[:100])
y_one.shape #(100, 2) : ์ถœ๋ ฅ์ˆ˜ : 2
print(y_one)

 



2. train_test_split

X_train, X_val, y_train, y_val = train_test_split(
    X, y_one, test_size=0.3, random_state=123)

 



3. keras model 

model = Sequential() #์ƒ์„ฑ์ž -> model ๊ฐ์ฒด



4. DNN model layer ๊ตฌ์ถ• 
1์ธต - hidden1 : ๋‰ด๋Ÿฐ 8๊ฐœ 
2์ธต - hidden2 : ๋‰ด๋Ÿฐ 4๊ฐœ 
3์ธต - output  : ๋‰ด๋Ÿฐ 2๊ฐœ

model.add(Dense(units=8, input_shape=(4,), activation='relu'))#1์ธต 
model.add(Dense(units=4, activation='relu')) #2์ธต 
model.add(Dense(units=2, activation='sigmoid')) #3์ธต


model layer ํ™•์ธ 

model.summary()

_________________________________________________________________
Layer (type)                 Output Shape              Param=(in*out)+b   
=================================================================
dense (Dense)                (None, 8)                 40=(4*8)+8        
_________________________________________________________________
dense_1 (Dense)              (None, 4)                 36=(8*4)+4        
_________________________________________________________________
dense_2 (Dense)              (None, 2)                 10=(4*2)+2       
=================================================================
Total params: 86

 


5. model compile : ํ•™์Šต๊ณผ์ • ์„ค์ •(์ดํ•ญ๋ถ„๋ฅ˜๊ธฐ)

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

optimizer : ์ตœ์ ํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜('adam', 'sgd')
loss : ์†์‹คํ•จ์ˆ˜('binary_crossentropy', 'categorical_crossentropy', 'mse')
metrics : ํ‰๊ฐ€๋ฐฉ๋ฒ•('accuracy', 'mae')


 

6. model training : train(70) vs val(30)

model.fit(x=X_train, y=y_train, #ํ›ˆ๋ จ์…‹ 
          epochs=30, #๋ฐ˜๋ณตํ•™์Šต ํšŸ์ˆ˜ 
          verbose=1, #์ถœ๋ ฅ์—ฌ๋ถ€ 
          validation_data= (X_val, y_val)) #๊ฒ€์ฆ์…‹

Epoch 30/30
3/3 [==============================] - 0s 4ms/step - loss: 0.4898 - accuracy: 1.0000 - val_loss: 0.4971 - val_accuracy: 0.9667



7. model evaluation : val dataset

print('='*30)
print('model evaluation')
model.evaluate(x=X_val, y=y_val)

==============================
model evaluation 1/1 0s 2ms/step - loss: 0.4971 - accuracy: 0.9667
์†๋„, ์†์‹ค๊ฐ’, ์ •ํ™•๋ฅ 

 

 

 

 

 

keras dnn category

- Keras model : ๋‹คํ•ญ๋ถ„๋ฅ˜๊ธฐ 
X๋ณ€์ˆ˜ : ์ •๊ทœํ™”(0~1)
Y๋ณ€์ˆ˜ : one hot encoding(2์ง„์ˆ˜)

from sklearn.datasets import load_iris #dataset 
from sklearn.model_selection import train_test_split #split 
from sklearn.preprocessing import minmax_scale #X๋ณ€์ˆ˜ : ์ •๊ทœํ™”(0~1)

from tensorflow.keras.utils import to_categorical #Y๋ณ€์ˆ˜ : encoding 
from tensorflow.keras import Sequential #keras model ์ƒ์„ฑ 
from tensorflow.keras.layers import Dense #DNN layer ๊ตฌ์ถ•




keras ๋‚ด๋ถ€ w,b๋ณ€์ˆ˜ seed ์ ์šฉ 

import tensorflow as tf
import numpy as np 
import random as rd

tf.random.set_seed(123)
np.random.seed(123)
rd.seed(123)




1. dataset load & ์ „์ฒ˜๋ฆฌ 

X, y = load_iris(return_X_y=True)

X.shape #(150, 4) : ์ž…๋ ฅ์ˆ˜=4
print(y) #0~2 : class


X๋ณ€์ˆ˜ : ์ •๊ทœํ™” 

X = minmax_scale(X) #[์ˆ˜์ •]
print(X)


Y๋ณ€์ˆ˜ : class -> one hot encoding

y_one = to_categorical(y) #[์ˆ˜์ •]
y_one.shape #(150, 3) : ์ถœ๋ ฅ์ˆ˜ : 3
print(y_one)




2. train_test_split

X_train, X_val, y_train, y_val = train_test_split(
    X, y, test_size=0.3, random_state=123) #y_one -> y

 



3. keras model 

model = Sequential() #์ƒ์„ฑ์ž -> model ๊ฐ์ฒด




4. DNN model layer ๊ตฌ์ถ• : [์ˆ˜์ •]
1์ธต - hidden1 : ๋‰ด๋Ÿฐ 8๊ฐœ -> 12๊ฐœ
2์ธต - hidden2 : ๋‰ด๋Ÿฐ 4๊ฐœ -> 6๊ฐœ 
3์ธต - output  : ๋‰ด๋Ÿฐ 2๊ฐœ -> 3๊ฐœ 

model.add(Dense(units=12, input_shape=(4,), activation='relu'))#1์ธต 
model.add(Dense(units=6, activation='relu')) #2์ธต 
model.add(Dense(units=3, activation='softmax')) #3์ธต : [์ˆ˜์ •]


model layer ํ™•์ธ 

model.summary()

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_42 (Dense)             (None, 12)                60        
_________________________________________________________________
dense_43 (Dense)             (None, 6)                 78        
_________________________________________________________________
dense_44 (Dense)             (None, 3)                 21        
============================================================
Total params: 159

 


5. model compile : ํ•™์Šต๊ณผ์ • ์„ค์ •(๋‹คํ•ญ๋ถ„๋ฅ˜๊ธฐ) 

model.compile(optimizer='adam', 
              #loss='categorical_crossentropy', #[์ˆ˜์ •] y : one_hot encoding 
              loss='sparse_categorical_crossentropy', #y : class(0,1,2)
              metrics=['accuracy'])

optimizer : ์ตœ์ ํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜('adam', 'sgd')
loss : ์†์‹คํ•จ์ˆ˜('binary_crossentropy', 'categorical_crossentropy', 'mse')
metrics : ํ‰๊ฐ€๋ฐฉ๋ฒ•('accuracy', 'mae')

์†์‹คํ•จ์ˆ˜ : y๋ณ€์ˆ˜ type์— ์˜ํ•ด์„œ ๊ฒฐ์ • 
y : one_hot encoding(100,010,001) -> lost = 'categorical_crossentropy'
y : class(0, 1, 2) -> lost = 'sparse_categorical_crossentropy'

 


6. model training : train(70) vs val(30)

model.fit(x=X_train, y=y_train, # ํ›ˆ๋ จ์…‹ 
          epochs=200, #๋ฐ˜๋ณตํ•™์Šต ํšŸ์ˆ˜ [์ˆ˜์ •]
          verbose=1, #์ถœ๋ ฅ์—ฌ๋ถ€ 
          validation_data= (X_val, y_val)) #๊ฒ€์ฆ์…‹

 

 


#7. model evaluation : val dataset 

print('='*30)
print('model evaluation')
model.evaluate(x=X_val, y=y_val)

epochs=100 : 0s 499us/step - loss: 0.3624 - accuracy: 0.8667
epochs=200 : 0s 499us/step - loss: 0.1601 - accuracy: 0.9556


 

8. model save & load : hdf5 ํŒŒ์ผ ํ˜•์‹ 

from tensorflow.keras.models import load_model #model load 
from sklearn.metrics import accuracy_score #model ํ‰๊ฐ€


1) model save

model.save(filepath='keras_model_iris.h5')


2) model load 

new_model = load_model(filepath='keras_model_iris.h5')




9. model test : test set 

X_train, X_test, y_train, y_test = train_test_split(
    X, y_one, test_size=0.5, random_state=123)

y_pred = new_model.predict(X_test) #new dataset 

y_pred #ํ™•๋ฅ ์˜ˆ์ธก : softmax ํ•จ์ˆ˜ 
y_pred.shape #(75, 3)


y_pred : ํ™•๋ฅ ์˜ˆ์ธก -> class ๋ณ€๊ฒฝ 

y_pred = tf.argmax(y_pred, axis=1) #ํ–‰๋‹จ์œ„ 
#y_true : one-hot encoding -> class ๋ณ€๊ฒฝ 
y_true = tf.argmax(y_test, axis=1) #ํ–‰๋‹จ์œ„

acc = accuracy_score(y_true, y_pred)
print('accuracy =',acc) #accuracy = 0.9733333333333334

 

 

 

 

 

keras dnn category learning rate

* keras dnn category ์ฐธ๊ณ  

- Keras model : ๋‹คํ•ญ๋ถ„๋ฅ˜๊ธฐ 
X๋ณ€์ˆ˜ : ์ •๊ทœํ™”(0~1)
Y๋ณ€์ˆ˜ : one hot encoding(2์ง„์ˆ˜)
optimizer='adam' -> optimizer=Adam(learning_rate=0.01)

from sklearn.datasets import load_iris #dataset 
from sklearn.model_selection import train_test_split #split 
from sklearn.preprocessing import minmax_scale #X๋ณ€์ˆ˜ : ์ •๊ทœํ™”(0~1)

from tensorflow.keras.utils import to_categorical #Y๋ณ€์ˆ˜ : encoding 
from tensorflow.keras import Sequential #keras model ์ƒ์„ฑ 
from tensorflow.keras.layers import Dense #DNN layer ๊ตฌ์ถ•



keras ๋‚ด๋ถ€ w,b๋ณ€์ˆ˜ seed ์ ์šฉ 

import tensorflow as tf
import numpy as np 
import random as rd

tf.random.set_seed(123)
np.random.seed(123)
rd.seed(123)

 

 


1. dataset load & ์ „์ฒ˜๋ฆฌ 

X, y = load_iris(return_X_y=True)

X.shape #(150, 4) : ์ž…๋ ฅ์ˆ˜=4


X๋ณ€์ˆ˜ : ์ •๊ทœํ™” 

X = minmax_scale(X) 
print(X)


Y๋ณ€์ˆ˜ : one hot encoding

y_one = to_categorical(y) 
y_one.shape #(150, 3) : ์ถœ๋ ฅ์ˆ˜ : 3
print(y_one)

 



2. train_test_split

X_train, X_val, y_train, y_val = train_test_split(
    X, y_one, test_size=0.3, random_state=123)

 



3. keras model 

model = Sequential() #์ƒ์„ฑ์ž -> model ๊ฐ์ฒด




4. DNN model layer ๊ตฌ์ถ• 
1์ธต - hidden1 : ๋‰ด๋Ÿฐ 8๊ฐœ -> 12๊ฐœ
2์ธต - hidden2 : ๋‰ด๋Ÿฐ 4๊ฐœ -> 6๊ฐœ 
3์ธต - output  : ๋‰ด๋Ÿฐ 2๊ฐœ -> 3๊ฐœ 

model.add(Dense(units=12, input_shape=(4,), activation='relu'))#1์ธต 
model.add(Dense(units=6, activation='relu')) #2์ธต 
model.add(Dense(units=3, activation='softmax')) #3์ธต


model layer ํ™•์ธ 

model.summary()

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_42 (Dense)             (None, 12)                60        
_________________________________________________________________
dense_43 (Dense)             (None, 6)                 78        
_________________________________________________________________
dense_44 (Dense)             (None, 3)                 21        
============================================================
Total params: 159


 

5. model compile : ํ•™์Šต๊ณผ์ • ์„ค์ •(๋‹คํ•ญ๋ถ„๋ฅ˜๊ธฐ) - [์ˆ˜์ •]

from tensorflow.keras import optimizers #Adam ์‚ฌ์šฉ 

#default : learning_rate=0.001
model.compile(optimizer=optimizers.Adam(learning_rate=0.01), #[์ˆ˜์ •]
              loss='categorical_crossentropy', 
              metrics=['accuracy'])

optimizer : ์ตœ์ ํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜('adam', 'sgd')
loss : ์†์‹คํ•จ์ˆ˜('binary_crossentropy', 'categorical_crossentropy', 'mse')
metrics : ํ‰๊ฐ€๋ฐฉ๋ฒ•('accuracy', 'mae')


 

6. model training : train(70) vs val(30)

model.fit(x=X_train, y=y_train, # ํ›ˆ๋ จ์…‹ 
          epochs=200, #๋ฐ˜๋ณตํ•™์Šต ํšŸ์ˆ˜ 
          verbose=1, #์ถœ๋ ฅ์—ฌ๋ถ€ 
          validation_data= (X_val, y_val)) #๊ฒ€์ฆ์…‹




7. model evaluation : val dataset 

print('='*30)
print('model evaluation')
model.evaluate(x=X_val, y=y_val)

epochs=100 : 0s 499us/step - loss: 0.3624 - accuracy: 0.8667
epochs=200(lr=0.001) : 0s 499us/step - loss: 0.1601 - accuracy: 0.9556
epochs=200(lr=0.01) : 0s 968us/step - loss: 0.0724 - accuracy: 0.9778

 

 

8. model save & load : hdf5 ํŒŒ์ผ ํ˜•์‹ 

from tensorflow.keras.models import load_model #model load 
from sklearn.metrics import accuracy_score #model ํ‰๊ฐ€


1) model save

model.save(filepath='keras_model_iris.h5')


2) model load 

new_model = load_model(filepath='keras_model_iris.h5')




9. model test : test set 

X_train, X_test, y_train, y_test = train_test_split(
    X, y_one, test_size=0.5, random_state=123)

y_pred = new_model.predict(X_test) #new dataset 

y_pred #ํ™•๋ฅ ์˜ˆ์ธก : softmax ํ•จ์ˆ˜ 
y_pred.shape #(75, 3)


y_pred : ํ™•๋ฅ ์˜ˆ์ธก -> class ๋ณ€๊ฒฝ 

y_pred = tf.argmax(y_pred, axis=1) #ํ–‰๋‹จ์œ„ 
#y_true : one-hot encoding -> class ๋ณ€๊ฒฝ 
y_true = tf.argmax(y_test, axis=1) #ํ–‰๋‹จ์œ„

acc = accuracy_score(y_true, y_pred)
print('accuracy =',acc)

accuracy = 0.9733333333333334 -> learning_rate=0.001
accuracy = 0.9866666666666667 -> learning_rate=0.01

 

 

 

 

 

keras mnist batch

1. Mnist dataset ๋‹คํ•ญ๋ถ„๋ฅ˜๊ธฐ 
   - X(images) : 2d(28x28) = 1d(784 pixel)
   - Y(label) : 0~9(10์ง„์ˆ˜)

2. Full batch vs Mini batch 
   - Full batch : ์ „์ฒด ํ›ˆ๋ จ์…‹ ๊ณต๊ธ‰ 
   - Mini batch : ํ›ˆ๋ จ์…‹ ๋ถ„ํ•  ๊ณต๊ธ‰(size ํฐ ๊ฒฝ์šฐ)

from tensorflow.keras.datasets import mnist # mnist load 
from tensorflow.keras.utils import to_categorical # Y๋ณ€์ˆ˜ : encoding 
from tensorflow.keras import Sequential # keras model ์ƒ์„ฑ 
from tensorflow.keras.layers import Dense # DNN layer ๊ตฌ์ถ•



import tensorflow as tf
import numpy as np 
import random as rd

tf.random.set_seed(123)
np.random.seed(123)
rd.seed(123)
import time #ํ•™์Šต ์†Œ์š” ์‹œ๊ฐ„ ์ธก์ •



1. mnist dataset load 

(x_train, y_train), (x_val, y_val) = mnist.load_data() #(images, labels)


images : X๋ณ€์ˆ˜ 

x_train.shape #(60000, 28, 28) - (size, h, w) : 2d ์ œ๊ณต 
x_val.shape #(10000, 28, 28)

x_train[0] #0~255
x_train.max() #255


labels : y๋ณ€์ˆ˜ 

y_train.shape #(60000,)
y_train[0] #5




2. X,y๋ณ€์ˆ˜ ์ „์ฒ˜๋ฆฌ 
1) X๋ณ€์ˆ˜ : ์ •๊ทœํ™” & reshape(2d -> 1d)

x_train = x_train / 255. #์ •๊ทœํ™” 
x_val = x_val / 255.

x_train[0]


reshape(2d -> 1d)

x_train = x_train.reshape(-1, 784) #(60000, 28*28)
x_val = x_val.reshape(-1, 784) #(10000, 28*28)


2) y๋ณ€์ˆ˜ : class(10์ง„์ˆ˜) -> one-hot encoding(2์ง„์ˆ˜)

y_train = to_categorical(y_train)
y_val = to_categorical(y_val)


์ „์ฒ˜๋ฆฌ ํ™•์ธ 

x_train.shape # (60000, 784)
y_train[0] #[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.] - 5
y_train.shape #(60000, 10)

start_time = time.time() #์†Œ์š” ์‹œ๊ฐ„ ์ฒดํฌ




3. keras model

model = Sequential()




4. DNN model layer ๊ตฌ์ถ• 
hidden layer1 : w[784, 128]

model.add(Dense(units=128, input_shape=(784,), activation='relu')) #1์ธต


hidden layer2 : w[128, 64]

model.add(Dense(units=64, activation='relu')) #2์ธต


hidden layer3 : w[64, 32]

model.add(Dense(units=32, activation='relu')) #3์ธต


output layer : w[32, 10]

model.add(Dense(units=10, activation='softmax')) #4์ธต


model layer ํ™•์ธ 

model.summary()

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_8 (Dense)              (None, 128)               100480=784x128+128    
_________________________________________________________________
dense_9 (Dense)              (None, 64)                8256      
_________________________________________________________________
dense_10 (Dense)             (None, 32)                2080      
_________________________________________________________________
dense_11 (Dense)             (None, 10)                330       
=================================================================
Total params: 111,146

 



5. model compile : ํ•™์Šต๊ณผ์ • ์„ค์ •(๋‹คํ•ญ๋ถ„๋ฅ˜๊ธฐ) 

model.compile(optimizer='adam', #default : learning_rate=0.001
              loss='categorical_crossentropy', 
              metrics=['accuracy'])




6. model training : train(70) vs val(30)

model.fit(x=x_train, y=y_train, #ํ›ˆ๋ จ์…‹ 
          epochs=10, #๋ฐ˜๋ณตํ•™์Šต ํšŸ์ˆ˜ : 60000 * 10 = 600,000 -> full batch
          batch_size=100, #1epoch(100 * 600) * 10 = 600,000 -> mini batch 
          verbose=1, #์ถœ๋ ฅ์—ฌ๋ถ€ 
          validation_data= (x_val, y_val)) #๊ฒ€์ฆ์…‹

stop_time = time.time() - start_time 

print('์†Œ์š”์‹œ๊ฐ„ : ', stop_time)

full batch 
accuracy: 0.9923 - val_loss: 0.0873 - val_accuracy: 0.9793
์†Œ์š”์‹œ๊ฐ„ :  25.037985801696777

mini batch 
accuracy: 0.9933 - val_loss: 0.0916 - val_accuracy: 0.9739
์†Œ์š”์‹œ๊ฐ„ :  11.333117961883545



7. model evaluation : val dataset 

print('model evaluation')
model.evaluate(x=x_val, y=y_val)
#loss: 0.0916 - accuracy: 0.9739

 

 

 

 

 

keras_mnist_flatten

1. Mnist dataset ๋‹คํ•ญ๋ถ„๋ฅ˜๊ธฐ 
   - X(images) : 2d(28x28) -> model ๊ณต๊ธ‰ 
   - Y(label) : 0~9(10์ง„์ˆ˜)

2. Full batch vs Mini batch 
   - Full batch : ์ „์ฒด ํ›ˆ๋ จ์…‹ ๊ณต๊ธ‰ 
   - Mini batch : ํ›ˆ๋ จ์…‹ ๋ถ„ํ•  ๊ณต๊ธ‰(size ํฐ ๊ฒฝ์šฐ)

3. Flatten layer : ์ฐจ์›์ผ์น˜(input : 2d -> 1d) 


from tensorflow.keras.datasets import mnist #mnist load 
from tensorflow.keras.utils import to_categorical #Y๋ณ€์ˆ˜ : encoding 
from tensorflow.keras import Sequential #keras model ์ƒ์„ฑ 
from tensorflow.keras.layers import Dense, Flatten  #DNN layer ๊ตฌ์ถ•

 



keras ๋‚ด๋ถ€ w,b๋ณ€์ˆ˜ seed ์ ์šฉ 

import tensorflow as tf
import numpy as np 
import random as rd

tf.random.set_seed(123)
np.random.seed(123)
rd.seed(123)
import time #ํ•™์Šต ์†Œ์š” ์‹œ๊ฐ„ ์ธก์ •

 



1. mnist dataset load 

(x_train, y_train), (x_val, y_val) = mnist.load_data() #(images, labels)


images : X๋ณ€์ˆ˜ 

x_train.shape #(60000, 28, 28) - (size, h, w) : 2d ์ œ๊ณต 
x_val.shape #(10000, 28, 28)

x_train[0] #0~255
x_train.max() #255


labels : y๋ณ€์ˆ˜ 

y_train.shape #(60000,)
y_train[0] #5




2. X,y๋ณ€์ˆ˜ ์ „์ฒ˜๋ฆฌ 
1) X๋ณ€์ˆ˜ : ์ •๊ทœํ™” 

x_train = x_train / 255. #์ •๊ทœํ™” 
x_val = x_val / 255.

x_train[0]

 

[์ƒ๋žต] reshape(2d -> 1d)
x_train = x_train.reshape(-1, 784) # (60000, 28*28)
x_val = x_val.reshape(-1, 784) # (10000, 28*28)


2) y๋ณ€์ˆ˜ : class(10์ง„์ˆ˜) -> one-hot encoding(2์ง„์ˆ˜)

y_train = to_categorical(y_train)
y_val = to_categorical(y_val)

 

์ „์ฒ˜๋ฆฌ ํ™•์ธ 

x_train.shape # (60000, 784)
y_train[0] # [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.] - 5
y_train.shape # (60000, 10)

start_time = time.time() #์†Œ์š” ์‹œ๊ฐ„ ์ฒดํŠธ




3. keras model

model = Sequential()




4. DNN model layer ๊ตฌ์ถ• 

input_shape = (28, 28) #[์ถ”๊ฐ€] 2d image ๊ณต๊ธ‰


[์ถ”๊ฐ€] flatten layer  

model.add(Flatten(input_shape=input_shape))  #2d -> 1d(784)


hidden layer1 : w[784, 128]

model.add(Dense(units=128, input_shape=(784,), activation='relu')) #1์ธต

 

hidden layer2 : w[128, 64]

model.add(Dense(units=64, activation='relu')) #2์ธต


hidden layer3 : w[64, 32]

model.add(Dense(units=32, activation='relu')) #3์ธต


output layer : w[32, 10]

model.add(Dense(units=10, activation='softmax')) #4์ธต


model layer ํ™•์ธ 

model.summary()

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_8 (Dense)              (None, 128)               100480=784x128+128    
_________________________________________________________________
dense_9 (Dense)              (None, 64)                8256      
_________________________________________________________________
dense_10 (Dense)             (None, 32)                2080      
_________________________________________________________________
dense_11 (Dense)             (None, 10)                330       
=================================================================
Total params: 111,146



5. model compile : ํ•™์Šต๊ณผ์ • ์„ค์ •(๋‹คํ•ญ๋ถ„๋ฅ˜๊ธฐ) 

model.compile(optimizer='adam', #default : learning_rate=0.001
              loss='categorical_crossentropy', 
              metrics=['accuracy'])




6. model training : train(70) vs val(30)

model.fit(x=x_train, y=y_train, #ํ›ˆ๋ จ์…‹ 
          epochs=10, #๋ฐ˜๋ณตํ•™์Šต ํšŸ์ˆ˜ : 60000 * 10 = 600,000 -> full batch
          batch_size=100, #1epoch(100 * 600) * 10 = 600,000 -> mini batch 
          verbose=1, #์ถœ๋ ฅ์—ฌ๋ถ€ 
          validation_data= (x_val, y_val)) #๊ฒ€์ฆ์…‹

stop_time = time.time() - start_time 

print('์†Œ์š”์‹œ๊ฐ„ : ', stop_time)

full batch 
accuracy: 0.9923 - val_loss: 0.0873 - val_accuracy: 0.9793
์†Œ์š”์‹œ๊ฐ„ :  25.037985801696777

mini batch 
accuracy: 0.9933 - val_loss: 0.0916 - val_accuracy: 0.9739
์†Œ์š”์‹œ๊ฐ„ :  11.333117961883545



7. model evaluation : val dataset 

print('model evaluation')
model.evaluate(x=x_val, y=y_val) #loss: 0.0916 - accuracy: 0.9739