Linear Regression ์ ๊ฒฝ๋ง
Keras DNN model(Linear Regression ์ ๊ฒฝ๋ง)
Hidden layer : 2๊ฐ(๋ด๋ฐ 2๊ฐ)
model = Sequential()
#hidden layer1 : shape = [2, 2]
model.add(Dense(2, input_shape=(2,), activation = 'relu')) # 1์ธต(hidden1)
#hidden layer2 : shape = [2, 2]
model.add(Dense(2, activation = 'relu')) # 2์ธต(hidden2)
#output layer : shape = [2, 1]
model.add(Dense(units=1))
#์ถ๋ ฅ์ธต(output)
keras model
High Level API : Keras - ์ธ๊ฐ์ด ์์ค ์ธ์ด
Low Level API : ๊ธฐ๊ณ์ด ์์ค ์ธ์ด
dataset & ๋ชจ๋ธ ํ๊ฐ
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
keras model
import tensorflow as tf
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)
y #0~1
2. train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size = 0.3, random_state=123)
* ์กฐ์ ๋ณ์(w, b) ์ ์ ์์ : model์์ ์๋ ์์ฑ
3. DNN model ์์ฑ
model = Sequential() #keras model
print(model) #Sequential class์์ ๋ง๋ค์ด์ง object
model์ layer์ถ๊ฐ
hidden layer 2๊ฐ : unit = 12, unit=6
hidden layer1 : unit = 12 -> w[4, 12], b=12
model.add(Dense(units=12, input_shape=(4,), activation='relu')) #1์ธต
hidden layer2 : unit = 6 -> w[12, 6], b=6
model.add(Dense(units=6, activation='relu')) #2์ธต
* 6๊ฐ์ ๋ด๋ฐ์ ์ํด 2์ธต ๋ ์ด์ด ์์ฑ. 1์ธต์์ ๋ฐ์ ์จ ์๋ฃ๊ฐ input๋๋ฏ๋ก, input_shape=(4,)์กฐ๊ฑด์ ํ์์น ์๋ค.
output layer : unit = 1 -> w[6, 1], b=1
model.add(Dense(units=1)) #3์ธต
model layer ๊ตฌ์กฐ ํ์ธ
print(model.summary())
_________________________________________________________________
Layer (type) Output Shape Param -> (in*out)+b
=================================================================
dense (Dense) (None, 12) 60 -> (4*12)+12
_________________________________________________________________
dense_1 (Dense) (None, 6) 78 -> (12*6)+6
_________________________________________________________________
dense_2 (Dense) (None, 1) 7 -> (6*1)+1
=================================================================
Total params: 145
Trainable params: 145
Non-trainable params: 0
_________________________________________________________________
4. model compile : ํ์ต๊ณผ์ ์ค์
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
* ์ด๋์ ๋ ์ ํํ ๋์ด์๋ ํ์ต๊ณผ์
optimizer : ์ต์ ํ ์๊ณ ๋ฆฌ์ฆ ('adam' or 'sgd')
loss : ์์คํจ์(mes : ํ๊ท ์ ๊ณฑ์ค์ฐจ)
metrics : ํ๊ฐ ๋ฐฉ๋ฒ ('mae' or 'mse')
5. model training : model ํ์ต
model.fit(x=X_train, y=y_train, #ํ๋ จ์
epochs=100, #๋ฐ๋ณตํ์ตํ์ (์ฌ์ฉ์๊ฐ ์กฐ์ ๊ฐ๋ฅ)
verbose=1, #ํ์ต๊ฒฐ๊ณผ ์ถ๋ ฅ์ฌ๋ถ Y
validation_data=(X_test, y_test)) #๊ฒ์ฆ์
Epoch 1/100
loss: 16.4911 - mae: 3.7187 - val_loss: 14.8984 - val_mae: 3.4226
:
Epoch 100/100
loss: 0.1761 - mae: 0.3791 - val_loss: 0.1872 - val_mae: 0.3978
* val_ ์ ๊ฒ์ฆ๊ฒฐ๊ณผ
keras_history
keras history : model ํ์ต๊ณผ์ ๊ณผ ๊ฒ์ฆ๊ณผ์ ์ ์์ค๊ฐ ๋ฑ์ ๊ธฐ์ตํ๋ ๊ธฐ๋ฅ
dataset & ๋ชจ๋ธ ํ๊ฐ
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
#keras model
import tensorflow as tf
from tensorflow.keras import Sequential #keras model
from tensorflow.keras.layers import Dense #DNN layer
#keras seed๊ฐ ์ง์
import numpy as np
import random as rd
keras ๋ด๋ถ ๊ฐ์ค์น seed ์ ์ฉ
tf.random.set_seed(123) #global seed
np.random.seed(123) #numpy seed
rd.seed(123) #random seed
1. dataset load
X, y = load_iris(return_X_y=True)
X.shape #(150, 4)
y #0~2
2. train_test_split : ํ๋ จ์
vs ๊ฒ์ฆ์
X_train, X_val, y_train, y_val = train_test_split(
X, y, test_size=0.3, random_state=123)
์กฐ์ ๋ณ์(w, b) ์ ์ ์์ : model์์ ์๋ ์์ฑ
3. DNN model ์์ฑ
model = Sequential() #keras model
print(model) #Sequential object
hidden layer 2๊ฐ : unit=12, unit=6
hidden layer1 : unit = 12 -> w[4, 12], b=12
model.add(Dense(units=12, input_shape=(4,), activation='relu')) #1์ธต
hidden layer2 : unit = 6 -> w[12, 6], b=6
model.add(Dense(units=6, activation='relu')) #2์ธต
output layer : unit = 1 -> w[6, 1], b=1
model.add(Dense(units=1)) #3์ธต
model layer ํ์ธ
print(model.summary())
_________________________________________________________________
Layer (type) Output Shape Param #(in*out)+b
=================================================================
dense (Dense) (None, 12) 60=(4*12)+12
_________________________________________________________________
dense_1 (Dense) (None, 6) 78=(12*6)+6
_________________________________________________________________
dense_2 (Dense) (None, 1) 7=(6*1)+1
=================================================================
Total params: 145
Trainable params: 145
Non-trainable params: 0
_________________________________________________________________
4. model compile : ํ์ต๊ณผ์ ์ค์
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
optimizer : ์ต์ ํ ์๊ณ ๋ฆฌ์ฆ('adam', 'sgd')
loss : ์์คํจ์(mse)
metrics : ํ๊ฐ๋ฐฉ๋ฒ('mae', 'mse')
[์์ ] 5. model training : model ํ์ต
model_fit = model.fit(x=X_train, y=y_train, #ํ๋ จ์
epochs=100, #ํ์ตํ์
verbose=1, #์ถ๋ ฅ์ฌ๋ถ
validation_data=(X_val, y_val)) #๊ฒ์ฆ์
6. model evaluation : model ํ๊ฐ
loss, mae = model.evaluate(X_val, y_val)
print('loss value =', loss)
print('mae =', mae )
training data : 70% ํ๋ จ์
validation data : 30% ๊ฒ์ฆ์
test data : ์ค์ ์
๋ฌด์ฉ dataset
[์ถ๊ฐ] 7. model history
print(model_fit.history.keys()) #key ํ์ธ
#dict_keys(['loss', 'mae', 'val_loss', 'val_mae'])
model_fit.history['loss'] #ํ๋ จ์
์์ค๊ฐ
model_fit.history['val_loss'] #๊ฒ์ฆ์
์์ค๊ฐ
loss vs val loss
import matplotlib.pyplot as plt
plt.plot(model_fit.history['loss'], 'y', label='training loss value')
plt.plot(model_fit.history['val_loss'], 'r', label='val loss value')
plt.xlabel('epochs')
plt.ylabel('loss value')
plt.legend(loc = 'best')
plt.show()
model ํ๊ฐ : mae vs val mae
plt.plot(model_fit.history['mae'], 'y', label='training mae value')
plt.plot(model_fit.history['val_mae'], 'r', label='val mae value')
plt.xlabel('epochs')
plt.ylabel('mae')
plt.legend(loc = 'best')
plt.show()
'๋ฐ์ดํฐ๋ถ์๊ฐ ๊ณผ์ > Tensorflow' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
DAY65. Tensorflow Keras model (1)dnn model (0) | 2021.12.22 |
---|---|
DAY64. Tensorflow Classification (Sigmoid, Softmax) (0) | 2021.12.21 |
DAY62. Tensorflow LinearRegression (2)ํ๊ท๋ชจ๋ธ (0) | 2021.12.17 |
DAY61. Tensorflow LinearRegression (1)function basic (๊ธฐ๋ณธํจ์) (0) | 2021.12.16 |
DAY60. Tensorflow Basic (1)์ค์น, ๊ธฐ๋ณธ (0) | 2021.12.15 |