데이터 전처리

Keras로 배우는 언어 모델링을 위한 순환 신경망(RNN)

David Cecchini

Data Scientist

텍스트 분류

텍스트 분류의 활용:

  • 뉴스 자동 분류
  • 비즈니스 문서 분류
  • 고객 지원 대기열 세분화
  • 그 외 다양함
Keras로 배우는 언어 모델링을 위한 순환 신경망(RNN)

이진 분류와의 변경점

이진에서 다중 클래스로 바뀌는 점:

  • 출력 변수 y의 형태
  • 출력층 유닛 수
  • 출력층 활성화 함수
  • 손실 함수
Keras로 배우는 언어 모델링을 위한 순환 신경망(RNN)

이진 분류와의 변경점

출력 변수 y의 형태:

  • 클래스의 원-핫 인코딩
# Example: num_classes = 3
y[0] = [0, 1, 0]
y.shape = (N, num_classes)

출력층 유닛 수:

# Output layer
model.add(Dense(num_classes))
Keras로 배우는 언어 모델링을 위한 순환 신경망(RNN)

이진 분류와의 변경점

선 위의 숫자와 공간의 숫자 차이를 통해 원-핫 인코딩의 적용을 보여줌

Keras로 배우는 언어 모델링을 위한 순환 신경망(RNN)

이진 분류와의 변경점

출력층의 활성화 함수:

  • softmax는 각 클래스의 확률을 반환합니다
# Output layer
model.add(Dense(num_classes, activation="softmax"))

손실 함수:

  • 이진 대신 범주형 크로스 엔트로피 사용
# Compile the model
model.compile(loss='categorical_crossentropy')
Keras로 배우는 언어 모델링을 위한 순환 신경망(RNN)

keras용 텍스트 범주 준비

y = ["sports", "economy", "data_science", "sports", "finance"]
# Transform to pandas series object
y_series = pd.Series(y, dtype="category")

# Print the category codes print(y_series.cat.codes)
0    3
1    1
2    0
3    3
4    2
Keras로 배우는 언어 모델링을 위한 순환 신경망(RNN)

y 전처리

from tensorflow.keras.utils import to_categorical

y = np.array([0, 1, 2]) # Change to categorical y_prep = to_categorical(y) print(y_prep)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
Keras로 배우는 언어 모델링을 위한 순환 신경망(RNN)

연습해 봅시다!

Keras로 배우는 언어 모델링을 위한 순환 신경망(RNN)

Preparing Video For Download...