I am using the fashion MNIST dataset to try to work this out:

I am using the data from the links:

Training : http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz

training set labels: http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz

test set images http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz

test set labels http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz

I use the code to open the dataset:

def load_mnist(path, kind='train'):
    import os
    import gzip
    import numpy as np

    """Load MNIST data from `path`"""
    labels_path = os.path.join(path,
                               % kind)
    images_path = os.path.join(path,
                               % kind)

    with gzip.open(labels_path, 'rb') as lbpath:
        labels = np.frombuffer(lbpath.read(), dtype=np.uint8,

    with gzip.open(images_path, 'rb') as imgpath:
        images = np.frombuffer(imgpath.read(), dtype=np.uint8,
                               offset=16).reshape(len(labels), 784)

    return images, labels

label = ['T-shirt/top',  'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt',
         'Sneaker', 'Bag', 'Ankle boot']

data_dir = './'
X_train, y_train = load_mnist('D:\book', kind='train')
X_test, y_test = load_mnist('D:\book', kind='t10k')

X_train = X_train.astype(np.float32) / 256.0
X_test = X_test.astype(np.float32) / 256.0

I am trying to build a Convolutional Neural Network with the following architecture: - Convolutional Layer with 32 filters with size of 3x3 - ReLU activation function - 2x2 MaxPooling - Convolutional Layer with 64 filters with size of 3x3 - ReLU activation function - 2x2 MaxPooling - Fully connected layer with 512 units and ReLU activation function - Softmax activation layer for output layer For 100 epochs using the SGD optimizer

My Code is:

X_train = X_train.reshape([60000, 28, 28, 1])
X_train = X_train.astype('float32') / 255.0
X_test = X_test.reshape([10000, 28, 28, 1])
X_test = X_test.astype('float32') / 255.0
model = Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=[28,28,1]))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
y_train = keras.utils.np_utils.to_categorical(y_train)
y_test = keras.utils.np_utils.to_categorical(y_test)
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=100)

But it is taking a lot of time for execution. It is like 30 minutes per epoch. I think I am doing something wrong in my code. Can someone help me figure that out?


3 Answers 3


There are various ways to reduce training time per epoch.

  • Divide the dataset into batches, like @raghav gaur suggest.
  • Train on GPU. By default, I think Keras will use CPU.
  • Reduce the size of the neural networks. Print out the number of parameters to get an idea about the size of your network.

A good practice for debugging is to overfit one batch of data with your model. If it can't do that, there's likely a bug in your model.


The main problem in your code is that you are passing the whole data at once ,instead of that you should use a batch size of 500 or some other value .There is a parameter of batch size in model.fit() as well. And you should also reduce the number of epochs.


You should also be able reduce the number of epochs you train for. In the .fit() method you can add your test data so you can see how well the model does on the test data at the end of each epoch. This can help you see when the model starts over fitting.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.