<jupyter_start><jupyter_text>[はじめてのニューラルネットワーク：分類問題の初歩](https://www.tensorflow.org/tutorials/keras/classification?hl=ja)<jupyter_code>import tensorflow as tf
from tensorflow import keras

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

%matplotlib inline
dataset = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = dataset.load_data()

print(train_images.shape)
print(test_images.shape)

class_name = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal',
             'Shirt', 'Sneaker', 'Bag', 'Ankle boot']<jupyter_output><empty_output><jupyter_text>Display Images<jupyter_code>fig, axes = plt.subplots(4, 2, figsize=(10,15))

for x in np.arange(0,8):
    row = int(x/2)
    col = int(x%2)
    axes[row, col].imshow(train_images[x])
    axes[row, col].set_title(class_name[train_labels[x]])<jupyter_output><empty_output><jupyter_text>Image Preprocessing<jupyter_code>train_images = train_images / 255.0
test_images = test_images /255.0<jupyter_output><empty_output><jupyter_text>Model construction<jupyter_code>model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=(28,28)))
model.add(keras.layers.Dense(128, activation='relu'))
model.add(keras.layers.Dense(10, activation='softmax'))

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10)<jupyter_output><empty_output><jupyter_text>Evaluate and Predict<jupyter_code>model.evaluate(test_images, test_labels)
predictions = model.predict(test_images)
plt.figure(figsize=(15,15))
for x in np.arange(25):
    plt.subplot(5,5,x+1)
    plt.imshow(test_images[x])
    plt.title(class_name[test_labels[x]] + ':' + class_name[int(predictions[x].argmax())])<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup