케라스를 이용한 머신러닝-뉴스기사분류
케라스를 이용하여 뉴스기사분류, IMDB감정분석, 주택가격 예측
3가지를 해보겠습니다.
- 뉴스기사분류
2개이상의 클래스를 분류하는 다중분류문제
각 데이터가 하나의 범주로 분류되기때문에
단일레이블 다중분류
from tensorflow.python.keras.datasets import reuters
(train_data, train_labels), (test_data, test_labels) = reuters.load_data(num_words=10000)#가장 자주등장하는 단어 1만개까지
len(train_data),len(test_data), train_data, train_labels,len(train_labels),type(train_data),type(train_labels[0]),type(train_data[0][1])
(8982,
2246,
array([list([1, 2, 2, 8, 43, 10, 447, 5, 25, 207, 270, 5, 3095, 111, 16, 369, 186, 90, 67, 7, 89, 5, 19, 102, 6, 19, 124, 15, 90, 67, 84, 22, 482, 26, 7, 48, 4, 49, 8, 864, 39, 209, 154, 6, 151, 6, 83, 11, 15, 22, 155, 11, 15, 7, 48, 9, 4579, 1005, 504, 6, 258, 6, 272, 11, 15, 22, 134, 44, 11, 15, 16, 8, 197, 1245, 90, 67, 52, 29, 209, 30, 32, 132, 6, 109, 15, 17, 12]),
list([1, 3267, 699, 3434, 2295, 56, 2, 7511, 9, 56, 3906, 1073, 81, 5, 1198, 57, 366, 737, 132, 20, 4093, 7, 2, 49, 2295, 2, 1037, 3267, 699, 3434, 8, 7, 10, 241, 16, 855, 129, 231, 783, 5, 4, 587, 2295, 2, 2, 775, 7, 48, 34, 191, 44, 35, 1795, 505, 17, 12]),
list([1, 53, 12, 284, 15, 14, 272, 26, 53, 959, 32, 818, 15, 14, 272, 26, 39, 684, 70, 11, 14, 12, 3886, 18, 180, 183, 187, 70, 11, 14, 102, 32, 11, 29, 53, 44, 704, 15, 14, 19, 758, 15, 53, 959, 47, 1013, 15, 14, 19, 132, 15, 39, 965, 32, 11, 14, 147, 72, 11, 180, 183, 187, 44, 11, 14, 102, 19, 11, 123, 186, 90, 67, 960, 4, 78, 13, 68, 467, 511, 110, 59, 89, 90, 67, 1390, 55, 2678, 92, 617, 80, 1274, 46, 905, 220, 13, 4, 346, 48, 235, 629, 5, 211, 5, 1118, 7, 2, 81, 5, 187, 11, 15, 9, 1709, 201, 5, 47, 3615, 18, 478, 4514, 5, 1118, 7, 232, 2, 71, 5, 160, 63, 11, 9, 2, 81, 5, 102, 59, 11, 17, 12]),
...,
list([1, 141, 3890, 387, 81, 8, 16, 1629, 10, 340, 1241, 850, 31, 56, 3890, 691, 9, 1241, 71, 9, 5985, 2, 2, 699, 2, 2, 2, 699, 244, 5945, 4, 49, 8, 4, 656, 850, 33, 2993, 9, 2139, 340, 3371, 1493, 9, 2, 22, 2, 1094, 687, 83, 35, 15, 257, 6, 57, 9190, 7, 4, 5956, 654, 5, 2, 6191, 1371, 4, 49, 8, 16, 369, 646, 6, 1076, 7, 124, 407, 17, 12]),
list([1, 53, 46, 957, 26, 14, 74, 132, 26, 39, 46, 258, 3614, 18, 14, 74, 134, 5131, 18, 88, 2321, 72, 11, 14, 1842, 32, 11, 123, 383, 89, 39, 46, 235, 10, 864, 728, 5, 258, 44, 11, 15, 22, 753, 9, 42, 92, 131, 728, 5, 69, 312, 11, 15, 22, 222, 2, 3237, 383, 48, 39, 74, 235, 10, 864, 276, 5, 61, 32, 11, 15, 21, 4, 211, 5, 126, 1072, 42, 92, 131, 46, 19, 352, 11, 15, 22, 710, 220, 9, 42, 92, 131, 276, 5, 59, 61, 11, 15, 22, 10, 455, 7, 1172, 137, 336, 1325, 6, 1532, 142, 971, 6463, 43, 359, 5, 4, 326, 753, 364, 17, 12]),
list([1, 227, 2406, 91, 2, 125, 2855, 21, 4, 3976, 76, 7, 4, 757, 481, 3976, 790, 5259, 5654, 9, 111, 149, 8, 7, 10, 76, 223, 51, 4, 417, 8, 1047, 91, 6917, 1688, 340, 7, 194, 9411, 6, 1894, 21, 127, 2151, 2394, 1456, 6, 3034, 4, 329, 433, 7, 65, 87, 1127, 10, 8219, 1475, 290, 9, 21, 567, 16, 1926, 24, 4, 76, 209, 30, 4033, 6655, 5654, 8, 4, 60, 8, 4, 966, 308, 40, 2575, 129, 2, 295, 277, 1071, 9, 24, 286, 2114, 234, 222, 9, 4, 906, 3994, 8519, 114, 5758, 1752, 7, 4, 113, 17, 12])],
dtype=object),
array([ 3, 4, 3, ..., 25, 3, 25], dtype=int64),
8982,
numpy.ndarray,
numpy.int64,
int)
from keras.utils.np_utils import to_categorical #numpy배열을 onehot해줌
one_hot_train_label=to_categorical(train_labels)
#one_hot_train_data=to_categorical(train_data) 불가! numpy만 가능
one_hot_train_label
Using TensorFlow backend.
array([[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., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]], dtype=float32)
train_data.shape
(8982,)
word_index = reuters.get_word_index()
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
decoded_newswire =' '.join([reverse_word_index.get(i-3, '?')for i in train_data[2]])
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/reuters_word_index.json
557056/550378 [==============================] - 0s 1us/step
decoded_newswire
'? shr 3 28 dlrs vs 22 cts shr diluted 2 99 dlrs vs 22 cts net 46 0 mln vs 3 328 000 avg shrs 14 0 mln vs 15 2 mln year shr 5 41 dlrs vs 1 56 dlrs shr diluted 4 94 dlrs vs 1 50 dlrs net 78 2 mln vs 25 9 mln avg shrs 14 5 mln vs 15 1 mln note earnings per share reflect the two for one split effective january 6 1987 per share amounts are calculated after preferred stock dividends loss continuing operations for the qtr 1986 includes gains of sale of investments in ? corp of 14 mln dlrs and associated companies of 4 189 000 less writedowns of investments in national ? inc of 11 8 mln and ? corp of 15 6 mln reuter 3'
len(train_labels), len(test_labels)
(8982, 2246)
train_labels, type(train_labels)
(array([ 3, 4, 3, ..., 25, 3, 25], dtype=int64), numpy.ndarray)
import numpy as np
#원핫 인코딩으로
#있는단어 1 없는단어 0으로 한 세트당 총 10000개의 차원을 만듬
def vectorize_sequences(sequences, dimension=10000):
results = np.zeros((len(sequences), dimension))
for i,sequence in enumerate(sequences):
results[i, sequence]=1.
return results
x_train=vectorize_sequences(train_data)
x_test=vectorize_sequences(test_data)
y_train=vectorize_sequences(train_labels, 46)#레이블이 0~45까지 있기때문
y_test =vectorize_sequences(test_labels, 46)
x_train.shape, x_test.shape, y_train.shape, y_test.shape
((8982, 10000), (2246, 10000), (8982, 46), (2246, 46))
여러 클래스를 분류하기 위해 큰수의 유닛을 사용
from tensorflow.python.keras import models
from tensorflow.python.keras import layers
model=models.Sequential()
model.add(layers.Dense(64, activation = 'relu', input_shape=(10000,)))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(46, activation='softmax'))#클래스가 46개 이므로 46차원의 벡터 출력.
#각 입력샘플마다 46개의 출력 클래스에 대한 확률을 출력. 46개의 출력값을 더하면 1이됨
model.compile(optimizer='rmsprop',
loss ='categorical_crossentropy',#예상 확률 분포와 진짜 레이블의 분포사이의 거리
metrics=['acc'])
x_val= x_train[:1000] #검증용(validation)
partial_x_train=x_train[1000:]#학습용
y_val = y_train[:1000]
partial_y_train = y_train[1000:]
history = model.fit(partial_x_train, partial_y_train, epochs=20, batch_size=512, validation_data=(x_val, y_val))#history에 각 측정값들이 저장됨
Train on 7982 samples, validate on 1000 samples
Epoch 1/20
WARNING:tensorflow:Entity <function Function._initialize_uninitialized_variables.<locals>.initialize_variables at 0x0000022FEE6AD4C8> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Num'
WARNING: Entity <function Function._initialize_uninitialized_variables.<locals>.initialize_variables at 0x0000022FEE6AD4C8> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Num'
7982/7982 [==============================] - 1s 177us/sample - loss: 2.6493 - acc: 0.4609 - val_loss: 1.7618 - val_acc: 0.6430
Epoch 2/20
7982/7982 [==============================] - 1s 98us/sample - loss: 1.4428 - acc: 0.6976 - val_loss: 1.2954 - val_acc: 0.7040
Epoch 3/20
7982/7982 [==============================] - 1s 104us/sample - loss: 1.0671 - acc: 0.7684 - val_loss: 1.1131 - val_acc: 0.7560
Epoch 4/20
7982/7982 [==============================] - 1s 106us/sample - loss: 0.8394 - acc: 0.8218 - val_loss: 1.0291 - val_acc: 0.7640
Epoch 5/20
7982/7982 [==============================] - 1s 108us/sample - loss: 0.6686 - acc: 0.8584 - val_loss: 0.9585 - val_acc: 0.8040
Epoch 6/20
7982/7982 [==============================] - 1s 119us/sample - loss: 0.5408 - acc: 0.8851 - val_loss: 0.8961 - val_acc: 0.8090
Epoch 7/20
7982/7982 [==============================] - 1s 113us/sample - loss: 0.4319 - acc: 0.9105 - val_loss: 0.9101 - val_acc: 0.8020
Epoch 8/20
7982/7982 [==============================] - 1s 100us/sample - loss: 0.3541 - acc: 0.9255 - val_loss: 0.8845 - val_acc: 0.8140
Epoch 9/20
7982/7982 [==============================] - 1s 107us/sample - loss: 0.2952 - acc: 0.9364 - val_loss: 0.8932 - val_acc: 0.8140
Epoch 10/20
7982/7982 [==============================] - 1s 100us/sample - loss: 0.2510 - acc: 0.9427 - val_loss: 0.8801 - val_acc: 0.8150
Epoch 11/20
7982/7982 [==============================] - 1s 97us/sample - loss: 0.2143 - acc: 0.9474 - val_loss: 0.9252 - val_acc: 0.8100
Epoch 12/20
7982/7982 [==============================] - 1s 106us/sample - loss: 0.1897 - acc: 0.9503 - val_loss: 0.9596 - val_acc: 0.8170
Epoch 13/20
7982/7982 [==============================] - 1s 99us/sample - loss: 0.1672 - acc: 0.9516 - val_loss: 0.9661 - val_acc: 0.8070
Epoch 14/20
7982/7982 [==============================] - 1s 101us/sample - loss: 0.1534 - acc: 0.9530 - val_loss: 0.9526 - val_acc: 0.8100
Epoch 15/20
7982/7982 [==============================] - 1s 99us/sample - loss: 0.1431 - acc: 0.9544 - val_loss: 0.9726 - val_acc: 0.8100
Epoch 16/20
7982/7982 [==============================] - 1s 102us/sample - loss: 0.1349 - acc: 0.9567 - val_loss: 1.0009 - val_acc: 0.8080
Epoch 17/20
7982/7982 [==============================] - 1s 103us/sample - loss: 0.1256 - acc: 0.9582 - val_loss: 1.0947 - val_acc: 0.7930
Epoch 18/20
7982/7982 [==============================] - 1s 105us/sample - loss: 0.1227 - acc: 0.9572 - val_loss: 1.0648 - val_acc: 0.8080
Epoch 19/20
7982/7982 [==============================] - 1s 100us/sample - loss: 0.1178 - acc: 0.9584 - val_loss: 1.0886 - val_acc: 0.7930
Epoch 20/20
7982/7982 [==============================] - 1s 101us/sample - loss: 0.1145 - acc: 0.9595 - val_loss: 1.0946 - val_acc: 0.8010
import matplotlib.pyplot as plt
loss=history.history['loss']
val_loss=history.history['val_loss']
epochs=range(1, len(loss)+1)
plt.plot(epochs, loss ,'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title("Training and Validation loss")
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
plt.clf() #그래프 초기화
acc=history.history['acc']
val_acc=history.history['val_acc']
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and Validation acc')
plt.xlabel('epochs')
plt.ylabel('acc')
plt.legend()
plt.show()
9~10번째 에포크부터 과대적합인것을 알수있다
#모델 다시 생성, 훈련, 평가
model=models.Sequential()
model.add(layers.Dense(64, activation = 'relu', input_shape=(10000,)))
model.add(layers.Dense(64, activation='relu'))#중간층의 히든유닛이 마지막 출력보다 너무 작으면 정보의 병목이 생긴다(저차원으로 너무 압축했기때문)
model.add(layers.Dense(46, activation='softmax'))
model.compile(optimizer='rmsprop',
loss ='categorical_crossentropy',
metrics=['acc'])
history = model.fit(partial_x_train, partial_y_train, epochs=9, batch_size=512, validation_data=(x_val, y_val))
test_loss, test_acc=model.evaluate(x_test, y_test)
Train on 7982 samples, validate on 1000 samples
Epoch 1/9
WARNING:tensorflow:Entity <function Function._initialize_uninitialized_variables.<locals>.initialize_variables at 0x0000022FE8DEE438> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Num'
WARNING: Entity <function Function._initialize_uninitialized_variables.<locals>.initialize_variables at 0x0000022FE8DEE438> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Num'
7982/7982 [==============================] - 1s 170us/sample - loss: 2.5722 - acc: 0.5159 - val_loss: 1.7262 - val_acc: 0.6360
Epoch 2/9
7982/7982 [==============================] - 1s 96us/sample - loss: 1.4192 - acc: 0.7026 - val_loss: 1.3024 - val_acc: 0.7200
Epoch 3/9
7982/7982 [==============================] - 1s 98us/sample - loss: 1.0483 - acc: 0.7771 - val_loss: 1.1247 - val_acc: 0.7640
Epoch 4/9
7982/7982 [==============================] - 1s 97us/sample - loss: 0.8208 - acc: 0.8299 - val_loss: 1.0470 - val_acc: 0.7780
Epoch 5/9
7982/7982 [==============================] - 1s 99us/sample - loss: 0.6563 - acc: 0.8627 - val_loss: 0.9630 - val_acc: 0.8020
Epoch 6/9
7982/7982 [==============================] - 1s 108us/sample - loss: 0.5231 - acc: 0.8954 - val_loss: 0.9206 - val_acc: 0.8060
Epoch 7/9
7982/7982 [==============================] - 1s 120us/sample - loss: 0.4213 - acc: 0.9132 - val_loss: 0.9022 - val_acc: 0.8110
Epoch 8/9
7982/7982 [==============================] - 1s 117us/sample - loss: 0.3410 - acc: 0.9267 - val_loss: 0.9496 - val_acc: 0.8030
Epoch 9/9
7982/7982 [==============================] - 1s 124us/sample - loss: 0.2838 - acc: 0.9396 - val_loss: 0.9198 - val_acc: 0.8140
2246/1 [====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 0s 124us/sample - loss: 1.9336 - acc: 0.7894
print('test_loss={} test_acc={}'.format(test_loss, test_acc))
test_loss=0.9702195142489507 test_acc=0.7894033789634705
정리
N개의 클래스로 다중분류를 하려면 네트워크의 마지막 Dense층의 크기는
N이여야 한다
단일레이블, 다중분류 문제는 softmax를 사용한다(N개의 클래스에 대한 확률분포 출력)
모델이 출력한 확률분포와 실제 분포사이의 거리를 최소화 하기위해
손실함수로 범주형 크로스엔트로피 사용
Comments