def logregToy(): #load data toy = scipy.io.loadmat('../data/toy.mat') toy_train = toy['toy_train'] toy_test = toy['toy_test'] toy_train_label = np.transpose(toy_train[0, :].astype(np.double)) toy_train_label[toy_train_label < 0] = 0.0 toy_train_x = toy_train[0:3, :].astype(np.double) toy_train_x[ 0, :] = 1.0 # adding row of 1s in X matrix to account for w0 term toy_test_label = np.transpose(toy_test[0, :].astype(np.double)) toy_test_label[toy_test_label < 0] = 0.0 toy_test_x = toy_test[0:3, :].astype(np.double) toy_test_x[ 0, :] = 1.0 # adding row of 1s in X matrix to account for w0 term #training coefficients regcoeff = [0, 0.1, 0.5] # without regularization : regcoeff = 0 # with regularization : regcoeff = 1 / 2*sigma^2 for r in regcoeff: print('with regularization coefficient ', r) logreg = LOGREG(r) trained_theta = logreg.train(toy_train_x, toy_train_label, 50) print("Training:") logreg.printClassification(toy_train_x, toy_train_label) print("Test:") logreg.printClassification(toy_test_x, toy_test_label) # plot for toy dataset figname = 'Toy dataset with r: {}'.format(r) fig = plt.figure(figname) plt.subplot(221) plot2D(plt, toy_train_x, toy_train_label, trained_theta, 'Training') plt.subplot(222) plot2D(plt, toy_test_x, toy_test_label, trained_theta, 'Testing') plot3D(plt, fig.add_subplot(223, projection='3d'), toy_train_x, toy_train_label, trained_theta, 'Training') plot3D(plt, fig.add_subplot(224, projection='3d'), toy_test_x, toy_test_label, trained_theta, 'Test') plt.show()
def logreg_image(train: np.ndarray, test: np.ndarray, regularization_coefficients: List[float], is_cifar: bool = False) -> None: ''' without reg : 0 with reg: regularization_coefficients = 1 / 2sigma^2 :param train: data and labels for classifier training :param test: data and labels for classifier test :param is_cifar: True if CIFAR dataset is used, False if MNIST ''' train_label = np.transpose(train[0, :].astype(np.double)) train_label[train_label < 0] = 0.0 train_x = train.astype(np.double) train_x[0, :] = 1.0 print("Dataset ballance in training {:.2f}%".format( 100 * np.sum(train_label) / len(train_label))) test_label = np.transpose(test[0, :].astype(np.double)) test_label[test_label < 0] = 0.0 test_x = test.astype(np.double) test_x[0, :] = 1.0 print("Dataset ballance in test {:.2f}%".format(100 * np.sum(test_label) / len(test_label))) for r in regularization_coefficients: logreg = LOGREG(r) print( 'Training a LOGREG classifier with regularization coefficient: {}'. format(r)) # training logreg.train(train_x, train_label, 50) print('Training') logreg.printClassification(train_x, train_label) print('Test') logreg.printClassification(test_x, test_label) visualizeClassification(train_x[1:, :], train_label, logreg.classify(train_x), 3 * 3, is_cifar, 'training with reg: {}'.format(r)) visualizeClassification(test_x[1:, :], test_label, logreg.classify(test_x), 3 * 3, is_cifar, 'test with reg: {}'.format(r))
def logregToy() -> None: # load data toy = scipy.io.loadmat(dataPath + 'toy.mat') toy_train = toy['toy_train'] toy_test = toy['toy_test'] train_label = np.transpose(toy_train[0, :].astype(np.double)) train_label[train_label < 0] = 0.0 train_x = toy_train[0:3, :].astype(np.double) train_x[0, :] = 1.0 # adding row of 1s in X matrix to account for w0 term test_label = np.transpose(toy_test[0, :].astype(np.double)) test_label[test_label < 0] = 0.0 test_x = toy_test[0:3, :].astype(np.double) test_x[0, :] = 1.0 # adding row of 1s in X matrix to account for w0 term # training coefficients regularization_coefficients = [0, 0.1, 1.0] # without regularization : regularization_coefficients = 0 # with regularization : regularization_coefficients = 1 / 2*sigma^2 for r in regularization_coefficients: print('with regularization coefficient ', r) logreg = LOGREG(r) trained_w = logreg.train(train_x, train_label, 50) print("Training:") logreg.printClassification(train_x, train_label) print("Test:") logreg.printClassification(test_x, test_label) print("PARS: {}".format(trained_w)) # plot for toy dataset figname = 'Toy dataset with r: {}'.format(r) fig = plt.figure(figname, figsize=(12, 12)) plt.tight_layout() plt.subplot(221) plot2D(plt, train_x, train_label, trained_w, 'Training') plt.subplot(222) plot2D(plt, test_x, test_label, trained_w, 'Testing') plot3D(plt, fig.add_subplot(223, projection='3d'), train_x, train_label, trained_w, 'Training') plot3D(plt, fig.add_subplot(224, projection='3d'), test_x, test_label, trained_w, 'Test') plt.tight_layout() plt.show()
def logregMNIST(train, test): ''' without reg : 0 with reg: regcoeff = 1 / 2sigma^2 :param train: :param test: :return: ''' regcoeff = [0, 0.1, 0.5] train_label = np.transpose(train[0, :].astype(np.double)) train_label[train_label < 0] = 0.0 train_x = train[0:, :].astype(np.double) train_x[0, :] = 1.0 test_label = np.transpose(test[0, :].astype(np.double)) test_label[test_label < 0] = 0.0 test_x = test[0:, :].astype(np.double) test_x[0, :] = 1.0 for r in regcoeff: logreg = LOGREG(r) print( 'Training a LOGREG classifier with regularization coefficient: {}'. format(r)) # training logreg.train(train_x, train_label, 50) print('Training') logreg.printClassification(train_x, train_label) print('Test') logreg.printClassification(test_x, test_label) visualizeClassification(train_x[1:, :], train_label, logreg.classify(train_x), 3 * 3, 'training with reg: {}'.format(r)) visualizeClassification(test_x[1:, :], test_label, logreg.classify(test_x), 3 * 3, 'test with reg: {}'.format(r))