<jupyter_start><jupyter_text><jupyter_code>from tensorflow.keras.datasets import reuters
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
(X_train, Y_train), (X_test, Y_test) = reuters.load_data(num_words=10000, test_split=0.2) # test data 비율 0.2
len(X_train)
len(X_test)
num_classes =max(Y_train) + 1
print('카테고리:{}'.format(num_classes)) # 0부터 시작하는 숫자로 카테고리 라벨을 부여
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
    X_train[0]])
Y_train[0]
decoded_newswire
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(X_train) # 훈련 데이터 벡터 변환
X_test = vectorize_sequences(X_test) # 테스트 데이터 벡터 변환
from keras.utils.np_utils import to_categorical

Y_train = to_categorical(Y_train)
Y_test = to_categorical(Y_test)
from keras import models
from 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'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy',metrics=['accuracy'])
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4) #데이터 손실이 4회 증가하면, 조기종료
mc = ModelCheckpoint('best_model.h5', monitor='val_acc', mode='max', verbose=1, save_best_only=True) #검증 데이터의 정확도가 이전보다 좋아질 경우에만!! only 모델저장!
history = model.fit(X_train, Y_train, batch_size=128, epochs=30, callbacks=[es,mc], validation_data=(X_test,Y_test))
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['accuracy']
val_acc = history.history['val_accuracy']

plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()

plt.show()
results = model.evaluate(X_test, Y_test)
predictions = model.predict(X_test)
predictions[0].shape
np.sum(predictions[0])
np.argmax(predictions[0])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14418777943368108, 'fraction_numerical': 0.01713859910581222, 'mean_word_length': 7.111782477341389, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '443658', 'license': 'MIT', 'n_tokens_mistral': 1112, 'n_tokens_neox': 1134, 'n_words': 213, 'path': 'colab/news_classification_more.ipynb', 'repo_name': 'cateto/python4NLP', 'size': '2684'}