반응형
멀티클래스 분류
멀티 클래스 분류 : 여러개(3개 이상)의 아웃풋에서 가장 확률 높은 클래스를 고르는 것
=> Input( shape=(4 , ))
Dense(3, activation='softmax')
- 분류 문제 : softmax
- 회귀 문제 : activation 명시 안해도 됨
iris데이터사용 - 품종 분류 문제
1. x, y
x = iris.data
y = iris.target
2. 변수 개수 확인
x.shape, y.shape
출력 : ((150, 4), (150,))
3. target 이름 확인
iris.target_names
=> 출력 :
4. train set / test set 분리
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_testi = train_test_split(x, y, test_size=0.1, random_state=2023)
5. 모델링 하기 위해 모듈 불러오기
import tensorflow as tf
from tensorflow import keras
import numpy as np
6. 전처리 : One-Hot Encoding⭐
from tensorflow.keras.utils import to_categorical
target의 형태를 클래스 수만큼 분류
class_n = len(np.unique(y_train))
y_train = to_categorical(y_train, class_n)
y_test = to_categorical(y_test, class_n)
7. 모델링⭐
- activation = 'softmax'
- loss='categorical_crossentropy'
#1. 세션 클리어
keras.backend.clear_session()
#2. 모델 선언
model = keras.models.Sequential()
#3. 레이어 조립
model.add(keras.layers.Input(shape=(4, )))
model.add(keras.layers.Dense(3, activation='softmax'))
#4. 컴파일
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
모델 학습
model.fit(x_train, y_train, epochs=10, verbose=1)
=> 출력 :
▪ 모델 예측
y_pred = model.predict(x_test)
▪ 예측 값, 실제 값 비교
y_pred[:10], y_test[:10]
=> 출력 :
🌟 히든 레이어 추가하여 모델링 !! 🌟
sequential API : 선형 회귀 + 히든 레이어
▪ 모델링
조건 :
- 히든 레이어 노드 수 : 32
- 히든레이어 2개 추가
1. 세션 클리어
keras.backend.clear_session()
#2. 모델 선언 : 레이어 블록을 쌓을 발판 생성
model = keras.models.Sequential()
#3. 레이어 블록 조립
model.add(keras.layers.Input(shape=(4,))) #변수가 4개임
model.add(keras.layers.Dense(32, activation='relu'))
model.add(keras.lyers.Dense(32, activation='relu'))
#4. 컴파일
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
▪ 모델 학습
model.fit(x_train, y_train, epochs=10, verbose=1)
=> 출력:
🔸 accuracy 값이 높아짐❗❗❗
- 히든 레이어 추가전 : 0.3407
- 히든 레이어 추가후 : 0.7037
▪ 모델 예측
y_pred = model.predict(x_test)
▪ 예측 값, 실제 값 비교
y_pred[:10], y_test[:10]
=> 출력:
결론 : 히든 레이어 추가해서 모델링하면 예측 성능이 더 좋아진다!
반응형
'AI > DeepLearning' 카테고리의 다른 글
CNN 개념 (0) | 2023.03.14 |
---|---|
multi-input & Concatenate layer / Add layer (0) | 2023.03.06 |
딥러닝 파라미터, 라이브러리 정리 (0) | 2023.03.06 |
딥러닝 - 로지스틱 회귀(Logistic Regression) (0) | 2023.03.01 |
딥러닝_선형회귀(Linear Regression), Sequential API, Functional API (0) | 2023.02.28 |