<jupyter_start><jupyter_text>多クラス分類の例題<jupyter_code>%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

import tensorflow as tf
from tensorflow.keras import layers, models, initializers
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.datasets import reuters


np.random.seed(20201231)
tf.random.set_seed(20201231)

print("tensorflow version : " + tf.__version__)<jupyter_output>tensorflow version : 2.3.1<jupyter_text>Load dataset<jupyter_code>(x_train, y_train), (x_test, y_test) = reuters.load_data(num_words=10000)
#評価データの作成
x_val = x_train[:2000]
x_train = x_train[2000:]
y_val = y_train[:2000]
y_train = y_train[2000:]
print('x_train.shape: ', x_train.shape)
print('y_train.shape: ', y_train.shape)
print('x_val.shape: ', x_val.shape)
print('y_val.shape: ', y_val.shape)
print('x_test.shape: ', x_test.shape)
print('y_test.shape: ', y_test.shape)

print('\nx_train')
print(x_train)

print('\ny_train')
print(y_train)
#データセット0番目の内容を文章に戻してみる
print('\nデータセット0番目の内容を文章に戻してみる')
word_index = reuters.get_word_index()
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
#0はパディング、1はシーケンスの開始、2が不明として予約されているため除外する
decoded_newswire = ' '.join([reverse_word_index.get(i - 3, '?') for i in x_train[0]])
print(decoded_newswire)<jupyter_output>データセット0番目の内容を文章に戻してみる
? shr 78 cts vs 86 cts net 265 0 mln vs 283 0 mln revs 3 7 billion vs 3 6 billion avg shrs 329 0 mln vs 319 0 mln reuter 3<jupyter_text>Modify input data<jupyter_code>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_val = vectorize_sequences(x_val)
x_test = vectorize_sequences(x_test)
print('\nx_train')
print(x_train)
print('\nx_test')
print(x_test)

#labelは46種類あるため、要素46のOneHotに変換する
y_train = to_categorical(y_train, 46)
y_val = to_categorical(y_val, 46)
y_test = to_categorical(y_test, 46)
print('\ny_train')
print(y_train)<jupyter_output>x_train
[[0. 1. 0. ... 0. 0. 0.]
 [0. 1. 0. ... 0. 0. 0.]
 [0. 1. 1. ... 0. 0. 0.]
 ...
 [0. 1. 1. ... 0. 0. 0.]
 [0. 1. 1. ... 0. 0. 0.]
 [0. 1. 1. ... 0. 0. 0.]]

x_test
[[0. 1. 1. ... 0. 0. 0.]
 [0. 1. 1. ... 0. 0. 0.]
 [0. 1. 1. ... 0. 0. 0.]
 ...
 [0. 1. 0. ... 0. 0. 0.]
 [0. 1. 1. ... 0. 0. 0.]
 [0. 1. 1. ... 0. 0. 0.]]

y_train
[[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.]]<jupyter_text>Create deep learning layer<jupyter_code>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='sigmoid'))
model.summary()

model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 64)                640064    
_________________________________________________________________
dense_1 (Dense)              (None, 64)                4160      
_________________________________________________________________
dense_2 (Dense)              (None, 46)                2990      
=================================================================
Total params: 647,214
Trainable params: 647,214
Non-trainable params: 0
_________________________________________________________________<jupyter_text>Trainning<jupyter_code>training = model.fit(x_train, y_train,
          validation_data=(x_val, y_val),
          batch_size=512,
          epochs=5)

plt.plot(training.history['accuracy'])
plt.plot(training.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
#loss
plt.plot(training.history['loss'])
plt.plot(training.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()<jupyter_output>Epoch 1/5
14/14 [==============================] - 0s 34ms/step - loss: 3.4374 - accuracy: 0.4400 - val_loss: 3.0413 - val_accuracy: 0.5060
Epoch 2/5
14/14 [==============================] - 0s 17ms/step - loss: 2.6877 - accuracy: 0.5096 - val_loss: 2.3814 - val_accuracy: 0.4860
Epoch 3/5
14/14 [==============================] - 0s 17ms/step - loss: 2.0072 - accuracy: 0.5130 - val_loss: 1.9396 - val_accuracy: 0.5130
Epoch 4/5
14/14 [==============================] - 0s 18ms/step - loss: 1.4940 - accuracy: 0.6295 - val_loss: 1.4489 - val_accuracy: 0.6720
Epoch 5/5
14/14 [==============================] - 0s 17ms/step - loss: 0.9318 - accuracy: 0.8157 - val_loss: 1.1979 - val_accuracy: 0.7475<jupyter_text>Evaluate<jupyter_code>results = model.evaluate(x_test, y_test)<jupyter_output>71/71 [==============================] - 0s 3ms/step - loss: 1.1905 - accuracy: 0.7382<jupyter_text>Save the model<jupyter_code>model.save('models\\multi_class.h5')
model.save('models\\multi_class_pb', save_format="tf")

print(model.input)
print(model.output)<jupyter_output>WARNING:tensorflow:From c:\users\naolu\documents\github\tensorflow_samples\.venv\lib\site-packages\tensorflow\python\training\tracking\tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
WARNING:tensorflow:From c:\users\naolu\documents\github\tensorflow_samples\.venv\lib\site-packages\tensorflow\python\training\tracking\tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
INFO:tensorflow:Assets written to: models\multi_class_pb\assets
Tensor("dense_input:0", shape=(None, 10000), dtype=float32)
Tensor("dense_2/Sigmoid:0", shape=(None, 46), dtype=float32)<jupyter_text>Predict<jupyter_code>model = models.load_model('models\\multi_class.h5')
prediction = model.predict(x_test)
print(np.argmax(prediction[0]))<jupyter_output>3
{'fraction_non_alphanumeric': 0.19212428857098907, 'fraction_numerical': 0.06706660513767113, 'mean_word_length': 4.360263808738664, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '79357', 'license': 'MIT', 'n_tokens_mistral': 2752, 'n_tokens_neox': 2375, 'n_words': 500, 'path': 'multi_classification.ipynb', 'repo_name': 'naonaorange/tensorflow_samples', 'size': '6501'}