Solving the Fashion MNIST with a simple neural network

Fashion MNIST dataset

The first 20 photos and labels of the Fashion MNIST training set.
  1. Dense neural networks
  2. Convolutional neural networks
See how each node is connected to every node in the adjacent layers?

Coding the neural network

%tensorflow_version 2.x
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
# load the fashion mnist dataset from keras
fashion_mnist = keras.datasets.fashion_mnist
(train_X, train_y),(test_X,test_y) = fashion_mnist.load_data()
# data preprocessing, scaling values from 0-255 to 0-1
train_X = train_X/255.0
test_X = test_X/255.0
model = tf.keras.models.Sequential([ 
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.fit(train_X, train_y, epochs=12)
loss, acc = model.evaluate(test_X, test_y, verbose = 1)
print('\\nTest accuracy: ', acc)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store