250x250
Notice
Recent Posts
Recent Comments
Link
코딩걸음마
[딥러닝] TensorFlow Classification 분류 예측모델 템플릿 본문
728x90
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets, preprocessing # Scikit-learn (sklearn)
df = pd.read_csv("경로/경로/파일명.csv")
df
target = df[['']].copy()
data = df.copy()
del data['']
from sklearn import model_selection
train_data, test_data, train_label, test_label = model_selection.train_test_split(data, target,
test_size=0.3,
random_state=0)
print(train_data.shape)
print(test_data.shape)
print(train_label.shape)
print(test_label.shape)
기초 전처리
One-Hot_encoding
from tensorflow.keras import utils
train_label = utils.to_categorical(train_label) # 0 or 1 -> one-hot vector
test_label = utils.to_categorical(test_label) # 0 or 1 -> one-hot vector
print(train_label.shape)
print(test_label.shape)
# from sklearn import preprocessing
# enc = preprocessing.OneHotEncoder(categories='auto') # Apply 'One-hot encoding' on labels (Single integer to One-hot vector)
# train_label = enc.fit_transform(train_label).toarray()
# test_label = enc.fit_transform(test_label).toarray()
Scaling
from sklearn.preprocessing import StandardScaler
standardScaler = StandardScaler()
standardScaler.fit(train_data) # You must fit with train data only.
train_data = standardScaler.transform(train_data)
test_data= standardScaler.transform(test_data)
import tensorflow as tf
# tf.keras 에 필요한 함수들이 모여있습니다.
from tensorflow.keras import datasets, utils
from tensorflow.keras import models, layers, activations, initializers, losses, optimizers, metrics
model = models.Sequential() # Build up the "Sequence" of layers (Linear stack of layers)
# Dense-layer (with he-initialization)
model.add(layers.Dense(input_dim=8, units=256, activation=None, kernel_initializer=initializers.he_uniform())) # he-uniform initialization
# model.add(layers.BatchNormalization()) # Use this line as if needed
model.add(layers.Activation('elu')) # elu or relu (or layers.ELU / layers.LeakyReLU)
model.add(layers.Dense(units=512, activation=None, kernel_initializer=initializers.he_uniform()))
model.add(layers.Activation('elu'))
model.add(layers.Dense(units=512, activation=None, kernel_initializer=initializers.he_uniform()))
model.add(layers.Activation('elu'))
model.add(layers.Dense(units=256, activation=None, kernel_initializer=initializers.he_uniform()))
model.add(layers.Activation('elu'))
model.add(layers.Dropout(rate=0.5)) # Dropout-layer
model.add(layers.Dense(units=2, activation='softmax')) # Apply softmax function on model's output
# "Compile" the model description (Configures the model for training)
model.compile(optimizer=optimizers.Adam(), # Please try the Adam-optimizer
loss=losses.categorical_crossentropy,
metrics=[metrics.categorical_accuracy]) # Precision / Recall / F1-Score 적용하기 @ https://j.mp/3cf3lbi
# "Fit" the model on training data
history = model.fit(train_data, train_label, batch_size=100, epochs=20, validation_split=0.3)
# "Evaluate" the model on test data
result = model.evaluate(test_data, test_label)
print('loss (cross-entropy) :', result[0])
print('test accuracy :', result[1])
check
history.history.keys()
시각화
정확도
acc = history.history['categorical_accuracy']
val_acc = history.history['val_categorical_accuracy']
x_len = np.arange(len(acc))
plt.plot(x_len, acc, marker='.', c='blue', label="Train-set Acc.")
plt.plot(x_len, val_acc, marker='.', c='red', label="Validation-set Acc.")
plt.legend(loc='upper right')
plt.grid()
plt.xlabel('epoch')
plt.ylabel('Accuracy')
plt.show()
Loss
loss = history.history['categorical_accuracy']
val_loss = history.history['val_categorical_accuracy']
x_len = np.arange(len(acc))
plt.plot(x_len, loss, marker='.', c='blue', label="Train-set loss.")
plt.plot(x_len, val_loss, marker='.', c='red', label="Validation-set loss.")
plt.legend(loc='upper right')
plt.grid()
plt.xlabel('epoch')
plt.ylabel('Cross-entropy')
plt.show()
예측하기
answer = np.argmax(model.predict(sample_data), axis=1)
728x90
'딥러닝 템플릿' 카테고리의 다른 글
[딥러닝] Pytorch binary_Classification 이진분류 예측모델 템플릿 (0) | 2022.07.01 |
---|---|
[딥러닝] TensorFlow Regression 회귀 예측모델 템플릿 (0) | 2022.07.01 |
Comments