DAY65. Tensorflow Keras model (1)dnn model
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