Hello World With Keras
KerasでMNIST手書き数字を分類する。
from keras.datasets import mnist from keras import models from keras import layers from keras.utils import to_categorical import matplotlib.pyplot as plt (train_images, train_labels), (test_images, test_labels) = mnist.load_data() network = models.Sequential() # 最初の隠れ層は512を設定する, 活性化関数をReluを設定する network.add(layers.Dense(512, activation="relu", input_shape=(28*28,))) # 出力10個、0-9まで10個の出力あるから network.add(layers.Dense(10, activation="softmax")) network.compile(optimizer="rmsprop", loss="categorical_crossentropy", metrics=["accuracy"]) train_images = train_images.reshape((60000, 28*28)) train_images = train_images.astype("float32") / 255 test_images = test_images.reshape((10000, 28*28)) test_images = test_images.astype("float32") / 255 # 整数のクラスベクトルから2値クラスの行列への変換します. # https://keras.io/ja/utils/#to_categorical categorical_train_labels = to_categorical(train_labels) categorical_test_labels = to_categorical(test_labels) # fit the model to its training data # the network iterate on the training data in mini-batches of 128 samples, 5 times over # each iteration over all the training data is called an epoch history = network.