import scipy.io as sio import numpy as np import math import matplotlib.pyplot as plt def displayData(X): # Display stacked small images m,npix = X.shape example_width = round(math.sqrt(npix)) example_height = (npix/example_width) display_rows = math.floor(math.sqrt(m)) display_cols = math.ceil(m/display_rows) pad = 1 display_array = np.ones((pad+display_rows*(example_height+pad),pad+display_cols*(example_width+pad))) curr_ex= 0 for j in range(1,int(display_rows+1)): for i in range(1,int(display_cols+1)): if curr_ex>m: break max_val = max(abs(X[curr_ex,:])) padjstart =int((j-1)*(example_height+pad)) padistart = int( (i-1)*(example_width+pad)) currimstack = X[curr_ex,:] creshape = currimstack.reshape((int(example_height),int(example_width))).T tt=1 display_array[padjstart:padjstart+int(example_height),padistart:padistart+int(example_width)]=creshape reshape(X[curr_ex,:],example_height,wxample_width)/max_val curr_ex = curr_ex+1 if curr_ex>m: break plt.imshow(display_array, cmap='gray') plt.show() return def sigmoid_func(z): # Return the value of the sigmoid function return float(1) / (1 + math.e**(-z)) def nnetpredict(Theta1, Theta2, X): # Compute the predictions of a 2-layer network trained by Theta1 and Theta2 m,nfeat = X.shape num_labels,nofhiddennodes = Theta2.shape #print 'nofhiddennodes', nofhiddennodes #print nfeat #print num_labels, nofhiddennodes Aappend = np.ones((m, nfeat + 1)) Aappend[:, 1:] = X #print 'A', Aappend.shape #print 'X', X.shape #a1 = [np.ones((m)), X] #print a1.shape #print 'theta1', Theta1.shape z2 = Theta1.dot(Aappend.T) a2 = sigmoid_func(z2) #print 'a2', len(a2) #print 'm', m #print 'Theta2', Theta2.shape #print 'z2', len(z2) A2append = np.ones((m,nofhiddennodes)) A2append[:,1:]= a2.T z3 = Theta2.dot(A2append.T) #print 'A2append', A2append.shape #print 'z3', len(z3) a3 = sigmoid_func(z3) # Find the maximum value and the index of the maximum #print a3 #print a3.shape a3T = a3.T maxprsample = a3T.max(axis=1) maxind = np.argmax(a3T,axis=1) #maxval = np.amax(a3) #maxind = int(np.argmax(a3,axis=0)) #print 'v i', maxval, maxind pred = maxind return pred # Set up the number of nodes input_layer_size = 400 hidden_layer_size = 25 num_labels = 10 # Load and visualize the data #THis contains the MNIST images of handwritten digits datastruct = sio.loadmat('week6data1.mat') X = datastruct['X'] y=datastruct['y'] nsamp,nfeat = X.shape #print np.mean(X[14,:]) #print X.shape #y = datastruct.y #Visualize 100 random images sel = np.random.permutation(nsamp) sel = sel[:100] #displayData(X[sel,:]) # LOad a set of pretrained weights #print sio.whosmat('week6weights.mat') wstruct = sio.loadmat('week6weights.mat') Theta1 = wstruct['Theta1'] Theta2 = wstruct['Theta2'] #print Theta1.shape, Theta2.shape # PRedict the class labels predlab = nnetpredict(Theta1, Theta2, X) predlab = predlab+1 yvec = y[0:nsamp,0] meanacc = np.mean(predlab==yvec) print 'Training accuracy', meanacc rp = np.random.permutation(nsamp) # PRedict one at a time: nofexample = 0 for i in range(1,nofexample): pred = nnetpredict(Theta1, Theta2, X[[rp[i]],:]) #if y[rp[i]]==10: # truclass = 0 #else: # trueclass = y[rp[i]] print 'sample y pred',y[rp[i]] , pred+1 displayData(X[[rp[i]],:]) #[nofsamp,nfeat]=X.shape #print nofsamp, nfeat #print y[0:10] #print min(y), max(y)