# Import the required modules from sklearn.cross_validation import train_test_split from sklearn.metrics import classification_report from sklearn import datasets from lasagne import layers from lasagne import init from lasagne import nonlinearities from lasagne.updates import sgd,nesterov_momentum from nolearn.lasagne import NeuralNet import numpy as np %pylab inline # Load the training and testing set dataset = datasets.fetch_mldata("MNIST Original") (trainX, testX, trainY, testY) = train_test_split((dataset.data / 255.0).astype(np.float32), dataset.target.astype(np.int32), test_size = 0.33) # Create the classifier clf = NeuralNet( layers=[ ('input', layers.InputLayer), ('hidden', layers.DenseLayer), ('output', layers.DenseLayer), ], input_shape = (None, trainX.shape[1]), hidden_num_units=100, output_num_units=10, output_nonlinearity=nonlinearities.softmax, update=nesterov_momentum, #update=sgd, update_learning_rate=0.01, update_momentum=0.9, regression=False, max_epochs=20, verbose=1, #W=init.Uniform() ) # Perform the training clf.fit(trainX, trainY) # Perform the predictions preds = clf.predict(testX) # and show the precision, recall and f1-score print classification_report(testY, preds) # Sidekick Function to display the image in python notebook def imshow(im_title, im): ''' This is function to display the image''' plt.figure() plt.title(im_title) plt.axis("off") if len(im.shape) == 2: plt.imshow(im, cmap = "gray") else: im_display = cv2.cvtColor(im, cv2.COLOR_RGB2BGR) plt.imshow(im_display) plt.show() # randomly select a few of the test instances for i in np.random.choice(np.arange(0, len(testY)), size = (10,)): # classify the digit pred = clf.predict(np.atleast_2d(testX[i])) # reshape the feature vector to be a 28x28 pixel image, then change # the data type to be an unsigned 8-bit integer image = (testX[i] * 255).reshape((28, 28)).astype("uint8") # show the image and prediction imshow("Actual digit is {0}, predicted {1}".format(testY[i], pred[0]), image)