def main(): # get arguments train_size, epochs, learning_rate, path = functions.read_argv() # get data train_data = functions.read_gz_idx(path + 'train-images-idx3-ubyte.gz') train_label = functions.read_gz_idx(path + 'train-labels-idx1-ubyte.gz') test_data = functions.read_gz_idx(path + 't10k-images-idx3-ubyte.gz') test_label = functions.read_gz_idx(path + 't10k-labels-idx1-ubyte.gz') train_data, train_label, test_data, test_label = mnist_data_preprocess( train_data, train_label, test_data, test_label, train_size) # start training model = vanilla_perceptron(len(train_data[0]), learning_rate, epochs) model.train(train_data, train_label) # model.print_weights() # classify prediction = model.classify(train_data) f1, p, r = functions.f1_score(prediction, train_label) print ' > train f1 score: %.3f\t' % (f1) # print ' > train f1 score: %.3f\tprecision: %.3f\trecall: %.3f' % (f1, p, r) prediction = model.classify(test_data) f1, p, r = functions.f1_score(prediction, test_label) print ' > test f1 score: %.3f\t' % (f1)
def main(): # get arguments regularization, feature_type, path = functions.read_argv() epochs, mini_batch_size = 200, 10000 stop_criteria = 0.0005 # get data train_data = functions.read_gz_idx(path+'train-images-idx3-ubyte.gz') train_label = functions.read_gz_idx(path+'train-labels-idx1-ubyte.gz') test_data = functions.read_gz_idx(path+'t10k-images-idx3-ubyte.gz') test_label = functions.read_gz_idx(path+'t10k-labels-idx1-ubyte.gz') # data preprocessing train_data, train_label, test_data, test_label = data_preprocess(train_data, train_label, test_data, test_label, feature_type) # model initialization model = StochasticGradientDescent(len(train_data[0]), regularization, mini_batch_size) # initialize list for plotting accuracy_train = [] accuracy_test = [] # start training prev_loss = 0 # for stopping criteria epoch = epochs # for plotting for e in range(epochs): # shuffle training data if batch if mini_batch_size == len(train_data): train_data, train_label = functions.unison_shuffle(train_data, train_label) # model fitting loss = model.fit(train_data, train_label, 0) # test the accuracy acc_train = functions.accuracy(model.classify(train_data), train_label)/100 acc_test = functions.accuracy(model.classify(test_data), test_label)/100 # record for plotting accuracy_train.append(acc_train) accuracy_test.append(acc_test) # log print ("epoch {0:3d}:\t Train loss: {1:8.4f},\t Train acc: {2:8.4f}, \tTest acc: {3:8.4f}".format( e+1, loss, acc_train, acc_test)) # stopping criteria if np.absolute(prev_loss-loss)<stop_criteria: epoch = e+1 break prev_loss = loss print ('End of Train & Test') print ('Plotting ... ') # plot to graph if regularization: title = 'GD Regularize '+feature_type else: title = 'GD '+feature_type functions.plot(title, [e for e in range(1, epoch+1)], accuracy_train, accuracy_test) print ("End Of The Program")