Deep learning 은 기본적으로 다음과 같은 과정을 거친다.
set data -> define a model -> compile -> fit -> evaluation -> predition
모델을 정의하는 부분이 핵심이고, 성능을 결정한다.
Binary classification
: 8 feature를 사용해서 모델과 맞는지 아닌지 binary 결과 예측.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.model_selection import train_test_split
# set data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=ratio)
# Define
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu')) # 12 nodes, 8 features (e.g. # of variants)
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid')) # output layer
# Compile
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# accuracy for a classification problem (binary data)
# Fit
model.fit(X_train, y_train, epochs=150, batch_size=10, verbose=0)
# totally, there are 150*10th parameter updates.
# verbose : 0=turn off
# epochs : # of trainings of whole dataset
# batch_size : # of samples in one batch for updating parameters
(e.g. 20 batches (updates) with 10 batch_size for 200 samples)
----------------[ training is over ]----------------
# Evaluate
_, accuracy = model.evaluate(X_test, y_test, verbose=0)
print('Accuracy: %.2f' % (accuracy*100))
# Predict
predictions = model.predict(X)
for i in range(5):
print('%s => %d (expected %d)' % (X[i].tolist(), predictions[i], y[i]))
Multi-class classification
: 4 feature를 사용해서 3가지 결과 중 어디에 가까운지 확률로 나타내는 예제.
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.utils import np_utils
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import LabelEncoder
from sklearn.pipeline import Pipeline
# load dataset
dataframe = pandas.read_csv("iris.data", header=None)
dataset = dataframe.values
X = dataset[:,0:4].astype(float)
Y = dataset[:,4]
# encode class values as integers
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)
# convert integers to dummy variables (i.e. one hot encoded)
dummy_y = np_utils.to_categorical(encoded_Y)
def baseline_model(): # 4 inputs -> [8 hidden nodes] -> 3 outputs
# create model
model = Sequential()
model.add(Dense(8, input_dim=4, activation='relu'))
model.add(Dense(3, activation='softmax')) # normalize the output value as probability
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
estimator = KerasClassifier(build_fn=baseline_model, epochs=200, batch_size=5, verbose=0)
# Evaluate with k-fold cross validation
kfold = KFold(n_splits=10, shuffle=True)
results = cross_val_score(estimator, X, dummy_y, cv=kfold)
print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
Accuracy: 97.33% (4.42%)
* Categorical data
- Integer (label) encoding : "red" is 1, "green" is 2, and "blue" is 3. (indexing 이라고 생각하면된다.)
from numpy import asarray
from sklearn.preprocessing import OrdinalEncoder
# define data
data = asarray([['red'], ['green'], ['blue']])
# define ordinal encoding
encoder = OrdinalEncoder()
# transform data
result = encoder.fit_transform(data)
print(result)
[['red']
['green']
['blue']]
[[2.]
[1.]
[0.]]
- One-hot encoding : "red" is [1,0,0], "green" is [0,1,0], and "blue" is [0,0,1]. (column name이 있는 table로 (0없다 1있다)로 생각하면 된다.)
from numpy import asarray
from sklearn.preprocessing import OneHotEncoder
# define data
data = asarray([['red'], ['green'], ['blue']])
# define one hot encoding
encoder = OneHotEncoder(sparse=False)
# transform data
onehot = encoder.fit_transform(data)
print(onehot)
[['red']
['green']
['blue']]
[[0. 0. 1.]
[0. 1. 0.]
[1. 0. 0.]]
Regression
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
df = pd.read_csv("housing.data", delim_whitespace=True, header=None)
dataset = df.values
# split into input (X) and output (Y) variables
X = dataset[:,0:13]
Y = dataset[:,13]
def baseline_model(): # 13 inputs -> [13 hidden nodes] -> 1 output
# create model
model = Sequential()
model.add(Dense(13, input_dim=13, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, kernel_initializer='normal'))
# Compile model
model.compile(loss='mean_squared_error', optimizer='adam')
return model
# evaluate model
estimator = KerasRegressor(build_fn=baseline_model, epochs=100, batch_size=5, verbose=0)
kfold = KFold(n_splits=10)
results = cross_val_score(estimator, X, Y, cv=kfold)
print("Baseline: %.2f (%.2f) MSE" % (results.mean(), results.std()))
Baseline: -32.65 (23.33) MSE
Note: The mean squared error is negative because scikit-learn inverts so that the metric is maximized instead of minimized. You can ignore the sign of the result.
# evaluate model with standardized dataset
estimators = []
estimators.append(('standardize', StandardScaler()))
estimators.append(('mlp', KerasRegressor(build_fn=baseline_model, epochs=50, batch_size=5, verbose=0)))
pipeline = Pipeline(estimators)
kfold = KFold(n_splits=10)
results = cross_val_score(pipeline, X, Y, cv=kfold)
print("Standardized: %.2f (%.2f) MSE" % (results.mean(), results.std()))
Standardized: -29.54 (27.87) MSE
Application
def larger_model(): # 13 inputs -> [13 -> 6] -> 1 output
# create model
model = Sequential()
model.add(Dense(13, input_dim=13, kernel_initializer='normal', activation='relu'))
model.add(Dense(6, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, kernel_initializer='normal'))
# Compile model
model.compile(loss='mean_squared_error', optimizer='adam')
return model
def wider_model(): # 13 inputs -> [20] -> 1 output
# create model
model = Sequential()
model.add(Dense(20, input_dim=13, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, kernel_initializer='normal'))
# Compile model
model.compile(loss='mean_squared_error', optimizer='adam')
return model
Reference
- https://machinelearningmastery.com/tutorial-first-neural-network-python-keras/
- https://machinelearningmastery.com/multi-class-classification-tutorial-keras-deep-learning-library/
- https://machinelearningmastery.com/regression-tutorial-keras-deep-learning-library-python/
- https://machinelearningmastery.com/grid-search-hyperparameters-deep-learning-models-python-keras/
'AI' 카테고리의 다른 글
Retina image (0) | 2023.06.04 |
---|---|
Sensitivity analysis (0) | 2023.03.31 |
Reference (0) | 2021.04.26 |
댓글