#!/usr/bin/env python # coding: utf-8 # ## Standard (Fully Connected) Neural Network # In[1]: #Use in Markup cell type #![alt text](imagename.png "Title") # ### Implementing Fully connected Neural Net # #### Loading Required packages and Data # In[2]: ###1. Load Data and Splot Data from keras.datasets import mnist from keras.models import Sequential from keras.layers.core import Dense, Activation from keras.utils import np_utils (X_train, Y_train), (X_test, Y_test) = mnist.load_data() # #### Preprocessing # In[3]: import matplotlib.pyplot as plt n = 10 # how many digits we will display plt.figure(figsize=(20, 4)) for i in range(n): # display original ax = plt.subplot(2, n, i + 1) plt.imshow(X_test[i].reshape(28, 28)) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.show() plt.close() # In[4]: print("Previous X_train shape: {} \nPrevious Y_train shape:{}".format(X_train.shape, Y_train.shape)) X_train = X_train.reshape(60000, 784) X_test = X_test.reshape(10000, 784) X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /= 255 X_test /= 255 classes = 10 Y_train = np_utils.to_categorical(Y_train, classes) Y_test = np_utils.to_categorical(Y_test, classes) print("New X_train shape: {} \nNew Y_train shape:{}".format(X_train.shape, Y_train.shape)) # #### Setting up parameters # In[5]: input_size = 784 batch_size = 200 hidden1 = 400 hidden2 = 20 epochs = 2 # #### Building the FCN Model # In[6]: ###4.Build the model model = Sequential() model.add(Dense(hidden1, input_dim=input_size, activation='relu')) # output = relu (dot (W, input) + bias) model.add(Dense(hidden2, activation='relu')) model.add(Dense(classes, activation='softmax')) # Compilation model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='sgd') model.summary() # #### Training The Model # In[7]: # Fitting on Data model.fit(X_train, Y_train, batch_size=batch_size, epochs=10, verbose=2) ###5.Test # #### Testing The Model # In[8]: score = model.evaluate(X_test, Y_test, verbose=1) print('\n''Test accuracy:', score[1]) mask = range(10,20) X_valid = X_test[mask] y_pred = model.predict_classes(X_valid) print(y_pred) plt.figure(figsize=(20, 4)) for i in range(n): # display original ax = plt.subplot(2, n, i + 1) plt.imshow(X_valid[i].reshape(28, 28)) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.show() plt.close() # In[ ]: