text: import numpy as np
  import pandas as pd
  import matplotlib.pyplot as plt
  # %matplotlib inline
  import keras
  from keras.datasets import mnist
  from keras.models import Sequential
  from keras.layers import Dense, Dropout, Activation, Flatten
  from keras.optimizers import Adam
  from keras.layers.normalization import BatchNormalization
  from keras.utils import np_utils
  from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D, GlobalAveragePooling2D
  from keras.layers.advanced_activations import LeakyReLU 
  from keras.preprocessing.image import ImageDataGenerator
  
  np.random.seed(44)
  import tensorflow as tf
  from keras.preprocessing.image import ImageDataGenerator
  
  
  #gpu configer
  from tensorflow.python.client import device_lib
  #device_lib.list_local_devices()
  
  print(tf.test.gpu_device_name())
  
  config = tf.ConfigProto( device_count = {'GPU': 1 , 'CPU': 56} ) #max: 1 gpu, 56 cpu
  
  batch_size = 6000
  num_classes = 10
  epochs = 2000
  # input image dimensions
  img_rows, img_cols = 28, 28
  # the data, split between train and test sets
  (X_train, y_train), (X_test, y_test) = mnist.load_data()
  print("X_train original shape", X_train.shape)
  print("y_train original shape", y_train.shape)
  print("X_test original shape", X_test.shape)
  print("y_test original shape", y_test.shape)
  
  plt.imshow(X_train[0], cmap='gray')
  plt.title('Class '+ str(y_train[0]))
  
  X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
  X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
  
  X_train = X_train.astype('float32')
  X_test = X_test.astype('float32')
  
  X_train/=255
  X_test/=255
  
  X_train.shape
  
  number_of_classes = 10
  
  Y_train = np_utils.to_categorical(y_train, number_of_classes)
  Y_test = np_utils.to_categorical(y_test, number_of_classes)
  
  y_train[0], Y_train[0]
  
  
  gen = ImageDataGenerator()
  
  test_gen = ImageDataGenerator()
  
  train_generator = gen.flow(X_train, Y_train, batch_size=64)
  test_generator = test_gen.flow(X_test, Y_test, batch_size=64)
  
  model = Sequential()
  
  model.add(Conv2D(filters = 32, kernel_size = (3,3),padding = 'Same', 
                   activation ='relu', input_shape = (28,28,1)))
  model.add(Conv2D(filters = 32, kernel_size = (3,3),strides=(1,1),padding = 'Same', 
                   activation ='relu'))
  model.add(Conv2D(filters = 32, kernel_size = (3,3),strides=(1,1),padding = 'Same', 
                   activation ='relu'))
  model.add(Conv2D(filters = 32, kernel_size = (5,5),strides=(1,1),padding = 'Same', 
                   activation ='relu'))
  model.add(Conv2D(filters = 32, kernel_size = (5,5),strides=(1,1),padding = 'Same', 
                   activation ='relu'))
  model.add(Conv2D(filters = 32, kernel_size = (5,5),strides=(1,1),padding = 'Same', 
                   activation ='relu'))
  model.add(Conv2D(filters = 32, kernel_size = (5,5),strides=(1,1),padding = 'Same', 
                   activation ='relu'))
  
  
  model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', 
                   activation ='relu'))
  model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', 
                   activation ='relu'))
  model.add(Conv2D(filters = 64, kernel_size = (3,3),strides=(1,1),padding = 'Same', 
                   activation ='relu'))
  
  
  model.add(Flatten())
  model.add(Dense(256, activation = "relu"))
  model.add(Dense(100, activation = "relu"))
  model.add(Dense(10, activation = "softmax"))
  
  model.summary()
  
  model.compile(loss='categorical_crossentropy', optimizer=Adam(), metrics=['accuracy'])
  
  keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
  
  from matplotlib import pyplot as plt
  history = model.fit_generator(train_generator, steps_per_epoch=60000//batch_size, epochs=epochs, 
                      validation_data=test_generator, validation_steps=1000000//batch_size)
  
  plt.plot(history.history['loss'])
  plt.plot(history.history['val_loss'])
  plt.title('model loss')
  plt.ylabel('loss')
  plt.xlabel('epoch')
  plt.legend(['train', 'val'], loc='upper left')
  plt.show()
  
  plt.plot(history.history['acc'])
  plt.plot(history.history['val_acc'])
  plt.title('model accuracy')
  plt.ylabel('accuracy')
  plt.xlabel('epoch')
  plt.legend(['train', 'val'], loc='upper left')
  plt.show()
  
  score = model.evaluate(X_test, Y_test)
  print()
  print('Test accuracy: ', score)
  
  
  epochs=10
  
  model.fit_generator(train_generator, steps_per_epoch=60000//batch_size, epochs=epochs, 
                      validation_data=test_generator, validation_steps=10000//batch_size)
  
  score = model.evaluate(X_test, Y_test)
  print()
  print('Test accuracy: ', score)
  
  model.fit_generator(train_generator, steps_per_epoch=60000//batch_size, epochs=epochs, 
                      validation_data=test_generator, validation_steps=10000//batch_size)
  
  score = model.evaluate(X_test, Y_test)
  print()
  print('Test accuracy: ', score)
  
  model.fit_generator(train_generator, steps_per_epoch=60000//batch_size, epochs=epochs, 
                      validation_data=test_generator, validation_steps=10000//batch_size)
  
  score = model.evaluate(X_test, Y_test)
  print()
  print('Test accuracy: ', score)
  
  model.fit_generator(train_generator, steps_per_epoch=60000//batch_size, epochs=epochs, 
                      validation_data=test_generator, validation_steps=10000//batch_size)
  
  score = model.evaluate(X_test, Y_test)
  print()
  print('Test accuracy: ', score)