상세 컨텐츠

본문 제목

TensorFlow

인공지능/딥러닝

by 2^7 2022. 6. 13. 16:42

본문

TensorFlow

데이터 흐름 프로그래밍을 위한 Open Source Software Library

Neural Network 같은 Machine Learning 프로그램에 활용

주요 특징

• Keras API를 활용하여 손쉬운 모델 빌드

• 플랫폼 관계 없이 모델을 학습시키고 배포 가능

• 빠른 프로토타입 제작과 디버깅 구현 가능


Keras

Python 기반의 Deep Learning Framework(Library)

내부적으로는 TensorFlow, Theano, CNTK 등의 Deep Learning 전용 엔진 구동

누구나 쉽게 Deep Learning Model 생성 가능

Keras 사용자는 복잡한 내부 엔진에 대하여 알지 못해도 됨

직관적인 API를 통하여 MLP, CNN, RNN 등의 모델 생성 가능

다중 입력 및 다중 출력 구성 가능


Tensor

Neural Network 학습의 기본 데이터 단위

  • 숫자(Numeric) 데이터를 담기 위한 컨테이너
  • 임의의 차원(Dimension) 또는 축(Rank)을 가짐

Tensor in NLP(Natural Language Processing)

  • 문장과 단어를 숫자 벡터로 매핑

Keras Modeling

import seaborn as sns

iris = sns.load_dataset('iris')

1. Data Preprocessing

1-1. iris.Species 빈도분석

iris.species.value_counts()


1-2. DataFrame to Array & Casting

iris_AR = iris.values

iris_AR

AR_X = iris_AR[:, 0:4].astype(float)
AR_y = iris_AR[:, 4]

AR_X.shape, AR_y.shape

((150, 4), (150,))


1-3. One Hot Encoding with sklearn & Keras

  • LabelEncoder( )
    • ['setosa', 'virginica', 'virsicolor'] to [0, 1, 2]
from sklearn.preprocessing import LabelEncoder

encoder =  LabelEncoder()
AR_yLBE = encoder.fit_transform(AR_y)

AR_yLBE

#One-Hot Encoding - to_categorical( )

from tensorflow.keras.utils import to_categorical

AR_yOHE = to_categorical(AR_yLBE)

AR_yOHE


1-4. train_test_split( )

import tensorflow
import keras
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(AR_X, AR_yOHE, 
                                                    test_size = 0.3, 
                                                    random_state = 2045) 

X_train.shape, X_test.shape, y_train.shape, y_test.shape

((105, 4), (45, 4), (105, 3), (45, 3))


2. Keras Modeling

2-1. Model Define

from tensorflow.keras import models 
from tensorflow.keras import layers

Model_iris = models.Sequential()

Model_iris.add(layers.Dense(16, activation = 'relu', input_shape = (4,)))
Model_iris.add(layers.Dense(8, activation = 'relu'))
Model_iris.add(layers.Dense(3, activation = 'softmax'))
Model_iris.summary()

from tensorflow.keras import utils

utils.plot_model(Model_iris,
                 show_shapes = True,
                 show_dtype = True)


2-2. Model Compile

Model_iris.compile(loss = 'categorical_crossentropy',
                   optimizer = 'adam',
                   metrics = ['accuracy'])

2-3.Model Fit

History_iris = Model_iris.fit(X_train, y_train,
                              epochs = 500,
                              batch_size = 7,
                              validation_data = (X_test, y_test))


2-4. 학습 결과 시각화

import matplotlib.pyplot as plt

plt.figure(figsize = (9, 6))
plt.ylim(0, 1.2)
plt.plot(History_iris.history['loss'])
plt.plot(History_iris.history['val_loss'])
plt.plot(History_iris.history['accuracy'])
plt.plot(History_iris.history['val_accuracy'])
plt.legend(['loss', 'val_loss', 'accuracy', 'val_accuracy'])
plt.grid()
plt.show()


2-5. Model Evaluate

loss, accuracy = Model_iris.evaluate(X_test, y_test)

print('Loss = {:.2f}'.format(loss))
print('Accuracy = {:.2f}'.format(accuracy))


2-6. Model Predict

import numpy as np
np.set_printoptions(suppress = True, precision = 5)

Model_iris.predict(X_test)

y_hat = np.argmax(Model_iris.predict(X_test), axis = 1)

y_hat

  • One-Hot Encoding to Array
    • np.argmax( ) : 다차원 배열의 차원에 따라 가장 큰 값의 인덱스를 반환
    • axis = 1 : 열기준
y = np.argmax(y_test, axis = 1)

y

#Confusion Matrix & Claasification Report

from sklearn.metrics import confusion_matrix, classification_report

confusion_matrix(y, y_hat)

print(classification_report(y, y_hat, 
                            target_names = ['setosa',
                                            'virginica',
                                            'versicolor']))


3. Model Save & Load

Model_iris.save('Model_iris.h5')

!ls -l

from google.colab import files

files.download('Model_iris.h5')
from tensorflow.keras.models import load_model

Model_local = load_model('Model_iris.h5')
np.argmax(Model_local.predict(X_test), axis = 1)


#Save to Mounted Google Drive Directory

Model_iris.save('/content/drive/My Drive/Colab Notebooks/models/001_Model_iris.h5')
#Load from Mounted Google Drive Directory

from tensorflow.keras.models import load_model

Model_google = load_model('/content/drive/My Drive/Colab Notebooks/models/001_Model_iris.h5')
728x90

관련글 더보기