기본 콘텐츠로 건너뛰기

Python Tensorflow Softmax


Softmax : 다중분류

예제
# softmax : 다중분류
# 분류 클래스 수만큼의 값이 산출됨
# 산출된 값을 합하면 1이 됨
#!pip install tensorflow
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

np.random.seed(0)
tf.random.set_seed(3)
from sklearn.model_selection import train_test_split
import pandas as pd
df = pd.read_csv('housing.csv',delim_whitespace=True,header=None)
012345678910111213
00.0063218.02.3100.5386.57565.24.09001296.015.3396.904.9824.0
10.027310.07.0700.4696.42178.94.96712242.017.8396.909.1421.6
20.027290.07.0700.4697.18561.14.96712242.017.8392.834.0334.7
30.032370.02.1800.4586.99845.86.06223222.018.7394.632.9433.4
40.069050.02.1800.4587.14754.26.06223222.018.7396.905.3336.2
.............................................
5010.062630.011.9300.5736.59369.12.47861273.021.0391.999.6722.4
5020.045270.011.9300.5736.12076.72.28751273.021.0396.909.0820.6
5030.060760.011.9300.5736.97691.02.16751273.021.0396.905.6423.9
5040.109590.011.9300.5736.79489.32.38891273.021.0393.456.4822.0
5050.047410.011.9300.5736.03080.82.50501273.021.0396.907.8811.9

506 rows × 14 columns

dataset = df.values

X = dataset[:,:13]
y = dataset[:,13]

X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=0)
X_train.shape # (354, 13)
X_test.shape # (152, 13)
y_train.shape # (354,)
y_test.shape # (152,)

model = Sequential()
model.add(Dense(30,input_dim=13, activation='relu'))
model.add(Dense(20,activation='relu')) # 히든레이어
model.add(Dense(10,activation='relu')) # 히든레이어
model.add(Dense(15,activation='relu')) # 히든레이어
model.add(Dense(1)) # 결과

model.summary()
Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense_2 (Dense)             (None, 30)                420       
                                                                 
 dense_3 (Dense)             (None, 20)                620       
                                                                 
 dense_4 (Dense)             (None, 10)                210       
                                                                 
 dense_5 (Dense)             (None, 15)                165       
                                                                 
 dense_6 (Dense)             (None, 1)                 16        
                                                                 
=================================================================
Total params: 1,431
Trainable params: 1,431
Non-trainable params: 0
_________________________________________________________________

# Loss함수 'adam' 사용
model.compile(loss='mse', optimizer='adam')

# 학습내역
history = model.fit(X_train,y_train,epochs=200,batch_size=10) # epoch : 반복횟수, batch_size : 10개의 입력을 처리하고 손실함수를 기반으로 가중치 조정, 현재 1개 입력은 13의 input을 말한다

# 인공신경망이 계산한 집값
model.predict(X_test).flatten()

# 학습내역
history.history['loss']

# 학습내역 그래프
plt.plot(history.history['loss'])
plt.show()

예제2
# Tensorflow를 사용한 지도학습, 다중분류
# 회귀(Regression), 분류(Classification)
# 회귀 : 출력 레이어의 뉴런의 수가 1개
# 분류 : 출력 레이어의 뉴런의 수가 클래스의 수와 같이 설정
# 출력함수 : 회귀(linear), 분류(softmax)
# 손실함수 : crossentropy(Log Loss)
# binary_crossentropy, categorical_crossentropy, sparse_crossentropy
# 회원분류, 비지도학습
# 이미지 분류 : CNN, 지도학습
# 오브젝트 탐지
# Tensorflow를 이용한 다중분류의 예
# iris 품종 분류
# 분류 클래스의 수 : 3
# feature의 수 : 4
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
iris = load_iris()
iris.keys() # 'data', 'target', 'target_names'
data = iris['data']
data.shape # (150, 4)
target = iris['target']
target.shape # (150,)
target_names = iris['target_names']
target_names.shape # (3,)
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(data, target, test_size=.2, random_state=0)
X_train.shape # (120, 4)
X_test.shape # (30, 4)
y_train.shape # (120,)
y_test.shape # (30,)
# 신경망
model = Sequential() # 빈 신경망
model.add(Dense(10,input_dim=4,activation='relu'))
model.add(Dense(20,activation='relu'))
model.add(Dense(30,activation='relu'))
model.add(Dense(20,activation='relu'))
model.add(Dense(10,activation='relu'))
model.add(Dense(3,activation='softmax')) # 다중분류는 Softmax
model.summary()
Model: "sequential_3"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense_6 (Dense)             (None, 10)                50        
                                                                 
 dense_7 (Dense)             (None, 20)                220       
                                                                 
 dense_8 (Dense)             (None, 30)                630       
                                                                 
 dense_9 (Dense)             (None, 20)                620       
                                                                 
 dense_10 (Dense)            (None, 10)                210       
                                                                 
 dense_11 (Dense)            (None, 3)                 33        
                                                                 
=================================================================
Total params: 1,763
Trainable params: 1,763
Non-trainable params: 0
_________________________________________________________________
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy']) # optimizer : 경사하강법의 종류, loss : 손실함수, 'sparse' : OneHotEncoding 방식으로 처리, metrics : 측정도구
# 학습
hist = model.fit(X_train, y_train, epochs=500, batch_size=10, validation_split=0.05) # epochs : 500번 반복, batch_size : input 10번 당 가중치 조정, validation_split : 
hist.history.keys() # dict_keys(['loss', 'accuracy', 'val_loss', 'val_accuracy'])
loss = hist.history['loss']
acc = hist.history['accuracy']
vloss = hist.history['val_loss']
vacc = hist.history['val_accuracy']
import matplotlib.pyplot as plt
plt.figure(figsize=(5,2))
plt.plot(loss,'r--',label='loss')
plt.plot(acc,'g.',label='accuracy')
plt.plot(vloss,'y--',label='val_loss')
plt.plot(vacc,'b.',label='val_accuracy')
plt.legend()
plt.show()

model.evaluate(X_test, y_test)
1/1 [==============================] - 0s 29ms/step - loss: 0.0353 - accuracy: 1.0000
# 추정
pred = model.predict(X_test)
pred.shape # (30, 3)
pred
sum(pred[0])
pred[0].argmax() # 2
target_names[2] # 'virginica'

예제3
# 신경망을 이용한 회귀 실습
# 손실함수 : mse
# Optimizer : adam
# 활성함수 : activation='relu'
# 출력함수 : activation='linear'
# 출력 레이어의 뉴런 수 : 1
def get_y(x):
    return 4 * x + 7
x = np.random.randint(-100, 100, 500)
y = get_y(x)
y.shape # (500,)
X = x.reshape(-1,1)
X_train,X_test,y_train,y_test = train_test_split(X, y, test_size=.2, random_state=0)
X_train.shape, X_test.shape, y_train.shape, y_test.shape # ((400,), (100,), (400,), (100,))
model = Sequential() # 빈 신경망
model.add(Dense(1,input_dim=1)) # 기본 activation='linear' 생략가능
model.add(Dense(10))
model.add(Dense(15))
model.add(Dense(1))
model.summary()
model.compile(loss='mse', optimizer='adam',metrics=['mae']) # mae(Mean Absolute Error) : 오차를 가지고 정확도를 계산
hist = model.fit(X_train,y_train,epochs=100,batch_size=10,validation_split=.1)
hist.history.keys() # dict_keys(['loss', 'mae', 'val_loss', 'val_mae'])
loss = hist.history['loss']
mae = hist.history['mae']
val_loss = hist.history['val_loss']
val_mae = hist.history['val_mae']
plt.plot(loss,label='loss')
plt.plot(mae,label='mae')
plt.plot(val_loss,label='val_loss')
plt.plot(val_mae,label='val_mae')
plt.legend()
plt.show()
# 테스트
get_y(-50) # -193
model.predict([[-50]]) # -193.00005
model.evaluate(X_test, y_test) # [7.292737524977611e-10, 2.1238327462924644e-05]

이 블로그의 인기 게시물