def prepareData(): raw_data, raw_label = loadTrainingData('dataset/train.csv') test_data = loadTestingData('dataset/test.csv') #nomalize the train & test data raw_data = nnet.normalization(raw_data) test_data = nnet.normalization(test_data) #split training data to training set & validating set training_data = raw_data[:37000,:] training_label = raw_label[:37000,] validating_data = raw_data[37000:,:] validating_label = raw_label[37000:,] return training_data.transpose(),training_label,validating_data.transpose(),validating_label,test_data.transpose()
def prepareData(): raw_data, raw_label = loadTrainingData('dataset/train.csv') test_data = loadTestingData('dataset/test.csv') #nomalize the train & test data raw_data = nnet.normalization(raw_data) test_data = nnet.normalization(test_data) #split training data to training set & validating set training_data = raw_data[:37000, :] training_label = raw_label[:37000, ] validating_data = raw_data[37000:, :] validating_label = raw_label[37000:, ] return training_data.transpose( ), training_label, validating_data.transpose( ), validating_label, test_data.transpose()
test_data = test_data[:,test_set[0].flatten()] #step2 divide training set into feature learning set & supervised training set labeled_set = np.nonzero(labels>=5) unlabeled_set = np.nonzero(labels<=4) labeled_dataset = data[:,(labels<=4).flatten()] labeled_labelset = labels[(labels<=4).flatten()] unlabeled_dataset = data[:,(labels>=5).flatten()] unlabeled_dataset = unlabeled_dataset[:,:unlabeled_dataset.shape[1]/3] print unlabeled_dataset.shape #step3 feature learning using sparse autoencoder #TODO move normalization to miscellaneous unlabeled_dataset = nnet.normalization(unlabeled_dataset) W = nnet.sparseAutoencoder(inputSize,hiddenSize,sparsityParam,lmd,beta,alpha,unlabeled_dataset) W1 = np.reshape(W[:hiddenSize*inputSize,], (hiddenSize, inputSize)) b1 = np.reshape(W[2*hiddenSize*inputSize:2*hiddenSize*inputSize+hiddenSize,],(hiddenSize,1)) #step4 plot the learned feature fig = plt.figure(2) for index in range(hiddenSize/10): weight = W1[index,:] weight = np.reshape(weight,(28,28)) ax = fig.add_subplot(5,4,1+index) ax.imshow(weight,mpl.cm.gray) plt.show()
label = sio.loadmat('dataset/mnist_dataset/mnist_train_labels.mat') data = image['mnist_train'] training_labels = label['mnist_train_labels'] training_data = data.transpose() #testing data image = sio.loadmat('dataset/mnist_dataset/mnist_test.mat') label = sio.loadmat('dataset/mnist_dataset/mnist_test_labels.mat') test_data = image['mnist_test'] test_labels = label['mnist_test_labels'] test_data = test_data.transpose() start = time.time() #step2 L1 feature learning using sparse autoencoder #TODO move normalization to miscellaneous training_data = nnet.normalization(training_data) #W = nnet.sparseAutoencoder(inputSize,hiddenSizeL1,sparsityParam,lmd,beta,alpha,training_data,iters=500) W = load_data('weightL1') W = np.array(W) W = W.transpose() #savedata(W,'weightL1') W11 = np.reshape(W[:hiddenSizeL1*inputSize,], (hiddenSizeL1, inputSize)) b11 = np.reshape(W[2*hiddenSizeL1*inputSize:2*hiddenSizeL1*inputSize+hiddenSizeL1,],(hiddenSizeL1,1)) #step3 L2 feature learning using sparse autoencoder training_a1 = nnet.sigmoid(W11.dot(training_data)+b11) #W = nnet.sparseAutoencoder(hiddenSizeL1,hiddenSizeL2,sparsityParam,lmd,beta,0.009,training_a1,iters=500) W = load_data('weightL2') W = np.array(W) W = W.transpose() #savedata(W,'weightL2')
numClasses = 10 hiddenSizeL1 = 5 sparsityParam = 0.1 beta = 3 lmd = 0.001 alpha = 0.07 # step1 load training data # TODO move normalization to miscellaneous file_path = "dataset/faces.mat" images = sio.loadmat(file_path) data = images["data"] data = data.transpose() # normalize data data = nnet.normalization(data) # split data to training set and testing set testing_lables = [] training_labels = [] for index in range(10): train = data[:, 90 * index : 90 * index + 80] test = data[:, 90 * index + 80 : 90 * (index + 1)] if index == 0: training_set = train testing_set = test else: training_set = np.c_[training_set, train] testing_set = np.c_[testing_set, test] labels = 10 * [index]
label = sio.loadmat('dataset/mnist_dataset/mnist_train_labels.mat') data = image['mnist_train'] training_labels = label['mnist_train_labels'] training_data = data.transpose() #testing data image = sio.loadmat('dataset/mnist_dataset/mnist_test.mat') label = sio.loadmat('dataset/mnist_dataset/mnist_test_labels.mat') test_data = image['mnist_test'] test_labels = label['mnist_test_labels'] test_data = test_data.transpose() start = time.time() #step2 L1 feature learning using sparse autoencoder #TODO move normalization to miscellaneous training_data = nnet.normalization(training_data) #W = nnet.sparseAutoencoder(inputSize,hiddenSizeL1,sparsityParam,lmd,beta,alpha,training_data,iters=500) W = load_data('weightL1') W = np.array(W) W = W.transpose() #savedata(W,'weightL1') W11 = np.reshape(W[:hiddenSizeL1 * inputSize, ], (hiddenSizeL1, inputSize)) b11 = np.reshape( W[2 * hiddenSizeL1 * inputSize:2 * hiddenSizeL1 * inputSize + hiddenSizeL1, ], (hiddenSizeL1, 1)) #step3 L2 feature learning using sparse autoencoder training_a1 = nnet.sigmoid(W11.dot(training_data) + b11) #W = nnet.sparseAutoencoder(hiddenSizeL1,hiddenSizeL2,sparsityParam,lmd,beta,0.009,training_a1,iters=500) W = load_data('weightL2') W = np.array(W)
numClasses = 10 hiddenSizeL1 = 5 sparsityParam = 0.1 beta = 3 lmd = 0.001 alpha = 0.07 #step1 load training data #TODO move normalization to miscellaneous file_path = 'dataset/faces.mat' images = sio.loadmat(file_path) data = images['data'] data = data.transpose() #normalize data data = nnet.normalization(data) #split data to training set and testing set testing_lables = [] training_labels = [] for index in range(10): train = data[:,90*index:90*index+80] test = data[:,90*index+80:90*(index+1)] if index == 0: training_set = train testing_set = test else: training_set = np.c_[training_set, train] testing_set = np.c_[testing_set, test] labels = 10*[index]