def test_kmm_softmax(): print("========== Test KMM for multiclass classification ==========") np.random.seed(random_seed()) (x_train, y_train), (x_test, y_test) = demo.load_iris() print("Number of training samples = {}".format(x_train.shape[0])) print("Number of testing samples = {}".format(x_test.shape[0])) clf = KMM(model_name="KMM_hinge", D=4, lbd=0.01, gamma=0.01, mode='batch', loss='hinge', num_kernels=4, batch_size=100, temperature=0.1, num_epochs=10, num_nested_epochs=1, learning_rate=0.001, learning_rate_mu=0.001, learning_rate_gamma=0.001, learning_rate_alpha=0.001, random_state=random_seed()) clf.fit(x_train, y_train) train_err = 1.0 - clf.score(x_train, y_train) test_err = 1.0 - clf.score(x_test, y_test) print("Training error = %.4f" % train_err) print("Testing error = %.4f" % test_err) clf = KMM(model_name="KMM_hinge", D=100, lbd=0.0, gamma=0.01, mode='online', loss='hinge', num_kernels=4, batch_size=100, temperature=0.1, num_nested_epochs=1, learning_rate=0.001, learning_rate_mu=0.001, learning_rate_gamma=0.001, learning_rate_alpha=0.001, random_state=random_seed(), verbose=1) clf.fit(x_train, y_train) print("Mistake rate = %.4f" % clf.mistake)
def test_kmm_cv(): print("========== Test cross-validation for KMM ==========") np.random.seed(random_seed()) (x_train, y_train), (x_test, y_test) = demo.load_iris() print("Number of training samples = {}".format(x_train.shape[0])) print("Number of testing samples = {}".format(x_test.shape[0])) x = np.vstack([x_train, x_test]) y = np.concatenate([y_train, y_test]) early_stopping = EarlyStopping(monitor='val_err', patience=2, verbose=1) filepath = os.path.join(model_dir(), "male/KMM/iris_{epoch:04d}_{val_err:.6f}.pkl") checkpoint = ModelCheckpoint(filepath, mode='min', monitor='val_err', verbose=0, save_best_only=True) clf = KMM(model_name="KMM_hinge", D=20, lbd=0.0, gamma=0.1, mode='batch', loss='hinge', num_kernels=3, batch_size=100, temperature=1.0, num_epochs=10, num_nested_epochs=1, learning_rate=0.1, learning_rate_mu=0.0, learning_rate_gamma=0.1, learning_rate_alpha=0.1, metrics=['loss', 'err'], callbacks=[early_stopping, checkpoint], cv=[-1] * x_train.shape[0] + [0] * x_test.shape[0], random_state=random_seed(), verbose=1) clf.fit(x, y) train_err = 1.0 - clf.score(x_train, y_train) test_err = 1.0 - clf.score(x_test, y_test) print("Training error = %.4f" % train_err) print("Testing error = %.4f" % test_err)
def test_kmm_syn2d(show=False, block_figure_on_end=False): print("========== Test KMM on 2D data ==========") np.random.seed(random_seed()) (x_train, y_train), (x_test, y_test) = demo.load_synthetic_2d() idx_train, idx_test = next( iter( StratifiedShuffleSplit(n_splits=1, test_size=40, random_state=random_seed()).split( x_train, y_train))) x0 = x_train[idx_train] y0 = y_train[idx_train] x1 = x_train[idx_test] y1 = y_train[idx_test] x = np.vstack([x0, x1]) y = np.concatenate([y0, y1]) early_stopping = EarlyStopping(monitor='val_loss', patience=2, verbose=1) filepath = os.path.join( model_dir(), "male/KMM/syn2d_data_{epoch:04d}_{val_err:.6f}.pkl") checkpoint = ModelCheckpoint(filepath, mode='min', monitor='val_err', verbose=0, save_best_only=True) display = Display(layout=(3, 1), dpi='auto', show=show, block_on_end=block_figure_on_end, monitor=[ { 'metrics': ['loss', 'val_loss'], 'type': 'line', 'labels': ["training loss", "validation loss"], 'title': "Learning losses", 'xlabel': "epoch", 'ylabel': "loss", }, { 'metrics': ['err', 'val_err'], 'type': 'line', 'title': "Learning errors", 'xlabel': "epoch", 'ylabel': "error", }, { 'metrics': ['err'], 'type': 'line', 'labels': ["training error"], 'title': "Learning errors", 'xlabel': "epoch", 'ylabel': "error", }, ]) clf = KMM(model_name="KMM_hinge", D=10, lbd=0.0, gamma=0.5, mode='batch', loss='hinge', num_kernels=4, batch_size=4, temperature=0.1, num_epochs=10, num_nested_epochs=0, learning_rate=0.001, learning_rate_mu=0.0, learning_rate_gamma=0.001, learning_rate_alpha=0.001, metrics=['loss', 'err'], callbacks=[display, early_stopping, checkpoint], cv=[-1] * x0.shape[0] + [0] * x1.shape[0], random_state=random_seed()) clf.fit(x, y) train_err = 1.0 - clf.score(x_train, y_train) print("Training error = %.4f" % train_err) if block_figure_on_end: # save predictions y_test_pred = clf.predict(x_test) x_test[x_test == 0] = 1e-4 dump_svmlight_file(x_test, y_test_pred, os.path.join( data_dir(), "demo/synthetic_2D_data_test_predict.libsvm"), zero_based=False)
def test_kmm_cv_disp(show=False, block_figure_on_end=False): print("========== Test cross-validation for KMM with Display ==========") np.random.seed(random_seed()) (x_train, y_train), (x_test, y_test) = demo.load_iris() print("Number of training samples = {}".format(x_train.shape[0])) print("Number of testing samples = {}".format(x_test.shape[0])) x = np.vstack([x_train, x_test]) y = np.concatenate([y_train, y_test]) early_stopping = EarlyStopping(monitor='val_err', patience=2, verbose=1) filepath = os.path.join(model_dir(), "male/KMM/iris_{epoch:04d}_{val_err:.6f}.pkl") checkpoint = ModelCheckpoint(filepath, mode='min', monitor='val_err', verbose=0, save_best_only=True) display = Display(layout=(3, 1), dpi='auto', show=show, block_on_end=block_figure_on_end, monitor=[ { 'metrics': ['loss', 'val_loss'], 'type': 'line', 'labels': ["training loss", "validation loss"], 'title': "Learning losses", 'xlabel': "epoch", 'ylabel': "loss", }, { 'metrics': ['err', 'val_err'], 'type': 'line', 'title': "Learning errors", 'xlabel': "epoch", 'ylabel': "error", }, { 'metrics': ['err'], 'type': 'line', 'labels': ["training error"], 'title': "Learning errors", 'xlabel': "epoch", 'ylabel': "error", }, ]) clf = KMM(model_name="KMM_hinge", D=20, lbd=0.0, gamma=0.1, mode='batch', loss='hinge', num_kernels=3, batch_size=100, temperature=1.0, num_epochs=10, num_nested_epochs=1, learning_rate=0.1, learning_rate_mu=0.0, learning_rate_gamma=0.1, learning_rate_alpha=0.1, metrics=['loss', 'err'], callbacks=[display, early_stopping, checkpoint], cv=[-1] * x_train.shape[0] + [0] * x_test.shape[0], random_state=random_seed(), verbose=1) clf.fit(x, y) train_err = 1.0 - clf.score(x_train, y_train) test_err = 1.0 - clf.score(x_test, y_test) print("Training error = %.4f" % train_err) print("Testing error = %.4f" % test_err)