from keras.datasets import mnist (train_images, train_labels), (test_images, test_labels) = mnist.load_data() # Training images train_images.shape # Training labels (classification) train_labels # View images from training set import matplotlib.pyplot as plt plt.imshow(train_images[1], cmap='gray') from keras import models from keras import layers # Create sequential network model network = models.Sequential() # Add a fully connected or dense layer with rectified linear unit activation # function network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,))) # Add a fully connected or dense layer which will return an array of 10 # probability socres (summing to 1) network.add(layers.Dense(10, activation='softmax')) network.compile( optimizer = 'rmsprop', loss = 'categorical_crossentropy', metrics = ['accuracy'], ) # Flatten training images train_images = train_images.reshape((60000, 28 * 28)) train_images = train_images.astype('float32') / 255 # Flatten testing images test_images = test_images.reshape((10000, 28 * 28)) test_images = test_images.astype('float32') / 255 from keras.utils import to_categorical train_labels = to_categorical(train_labels) test_labels = to_categorical(test_labels) # epochs: How many times to go through dataset # batch_size: Size of each batch (how many to process in parallel) network.fit(train_images, train_labels, epochs=5, batch_size=128) test_loss, test_acc = network.evaluate(test_images, test_labels) print('Test Accurancy = {:.2f}%'.format(test_acc*100.0))