1.레이블링된 y값을 tf.keras.utils.to_categorical 함수 이용해서 원핫인코딩으로 바꾸기
from keras.utils import to_categorical
y_train
[out]
array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)
y_train = tf.keras.utils.to_categorical(y_train, num_classes = 10)
[out]
array([[0., 0., 0., ..., 0., 0., 0.],
[1., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 1., 0.]], dtype=float32)
2.분류의 문제에서, loss 함수를 categorical_crossentropy로 설정할때는 어떤상황일때?
model.compile('adam', loss='categorical_crossentropy', metrics=['accuracy']) # 원핫인코딩을 직접 해주었기 때문에 sparse를 제외한다.
sparse_categorical_crossentropy 는 자동으로 카테고리컬 데이터를 원핫 인코딩 해주는것이다.