<jupyter_start><jupyter_text>Implementing dropout regularization on Fashion MNIST dataset using MLP<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
import keras # Don't import keras from tensorflow like (from tensorflow import keras)
import seaborn as sns
# Reading train and test data
train_data = pd.read_csv('fashion-mnist_train.csv')
test_data = pd.read_csv('fashion-mnist_test.csv')
# Class names
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

# Making validation data out of test data
val_data = test_data.iloc[:5000,:]
test_data = test_data.iloc[5000:,:]

# Fetching the labels
train_labels = train_data.label
val_labels = val_data.label
test_labels = test_data.label

# Reshaping training data
train_images = train_data.iloc[:,1:].values.reshape(60000, 28, 28)

# Reshaping validation data
val_images = val_data.iloc[:,1:].values.reshape(5000, 28, 28)

# Reshaping test data
test_images = test_data.iloc[:,1:].values.reshape(5000, 28, 28)

# Scaling data in the range of 0-1
train_images = train_images/255.0
val_images = val_images/255.0
test_images = test_images/255.0
model = keras.Sequential([
        keras.layers.Flatten(input_shape=(28, 28)),   
        # Dropout layer can also be added between input layer and first hidden layer.
        # keras.layers.Dropout(0.30),
        
        # According to dropout paper, the maximum norm of the weights should not exceed a value of 3. 
        # Hence, defining constraint maxnorm(3).
        keras.layers.Dense(256, activation=tf.nn.relu, 
                           kernel_constraint=keras.constraints.maxnorm(3)), 
        keras.layers.Dropout(0.30), # Dropout layer with 30% of input units of last layer to drop
        keras.layers.Dense(10, activation=tf.nn.softmax) 
        ]) 

# Defining parameters like optmizer, loss function and evaluating metric
model.compile(loss='sparse_categorical_crossentropy', # 
        optimizer=keras.optimizers.Adam(), # Learning rate and momentum can be passed inside optimizer
        metrics=['accuracy'])

epochs = 5
model.fit(train_images, train_labels, epochs=epochs, 
        validation_data=(val_images, val_labels))<jupyter_output>Train on 60000 samples, validate on 5000 samples
Epoch 1/5
60000/60000 [==============================] - 15s 257us/step - loss: 0.5315 - acc: 0.8111 - val_loss: 0.3935 - val_acc: 0.8592
Epoch 2/5
60000/60000 [==============================] - 16s 268us/step - loss: 0.4070 - acc: 0.8516 - val_loss: 0.3652 - val_acc: 0.8624
Epoch 3/5
60000/60000 [==============================] - 16s 264us/step - loss: 0.3761 - acc: 0.8630 - val_loss: 0.3403 - val_acc: 0.8736
Epoch 4/5
60000/60000 [==============================] - 15s 245us/step - loss: 0.3550 - acc: 0.8715 - val_loss: 0.3281 - val_acc: 0.8796
Epoch 5/5
60000/60000 [==============================] - 14s 227us/step - loss: 0.3421 - acc: 0.8750 - val_loss: 0.3295 - val_acc: 0.8766
StarcoderdataJupyterStructuredCleanDedup