def test_rrf_cv_gridsearch(): print( "========== Tune parameters for RRF including cross-validation ==========" ) 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, x_test]) y = np.concatenate([y_train, y_test, y_test]) params = {'gamma': [0.5, 1.0], 'learning_rate': [0.01, 0.05, 0.1]} ps = PredefinedSplit(test_fold=[-1] * x_train.shape[0] + [-1] * x_test.shape[0] + [1] * x_test.shape[0]) early_stopping = EarlyStopping(monitor='val_err', patience=2) filepath = os.path.join( model_dir(), "male/RRF/search/mnist_{epoch:04d}_{val_err:.6f}.pkl") checkpoint = ModelCheckpoint(filepath, mode='min', monitor='val_err', verbose=0, save_best_only=True) clf = RRF(model_name="RRF_hinge", D=100, lbd=0.01, gamma=0.125, mode='batch', loss='hinge', num_epochs=10, learning_rate=0.001, learning_rate_gamma=0.001, metrics=['loss', 'err'], callbacks=[early_stopping, checkpoint], cv=[-1] * x_train.shape[0] + [0] * x_test.shape[0], catch_exception=True, random_state=random_seed()) gs = GridSearchCV(clf, params, cv=ps, n_jobs=-1, refit=False, verbose=True) gs.fit(x, y) print("Best error {} @ params {}".format(1 - gs.best_score_, gs.best_params_)) best_clf = clone(clf).set_params(**gs.best_params_) best_clf.fit(np.vstack([x_train, x_test]), np.concatenate([y_train, y_test])) train_err = 1.0 - best_clf.score(x_train, y_train) test_err = 1.0 - best_clf.score(x_test, y_test) print("Training error = %.4f" % train_err) print("Testing error = %.4f" % test_err) assert abs(test_err - (1.0 - gs.best_score_)) < 1e-4
def test_wgan_cifar10(show_figure=False, block_figure_on_end=False): print("========== Test WGAN on CIFAR10 data ==========") np.random.seed(random_seed()) num_data = 128 (x_train, y_train), (x_test, y_test) = demo.load_cifar10() x_train = x_train[:num_data].astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1. x_test = x_test.astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1. root_dir = os.path.join(model_dir(), "male/WGAN/CIFAR10") loss_display = Display(layout=(1, 1), dpi='auto', show=show_figure, block_on_end=block_figure_on_end, filepath=[os.path.join(root_dir, "loss/loss_{epoch:04d}.png"), os.path.join(root_dir, "loss/loss_{epoch:04d}.pdf")], monitor=[{'metrics': ['d_loss', 'g_loss'], 'type': 'line', 'labels': ["discriminator loss", "generator loss"], 'title': "Losses", 'xlabel': "epoch", 'ylabel': "loss", }, ]) sample_display = Display(layout=(1, 1), dpi='auto', figsize=(10, 10), freq=1, show=show_figure, block_on_end=block_figure_on_end, filepath=os.path.join(root_dir, "samples/samples_{epoch:04d}.png"), monitor=[{'metrics': ['x_samples'], 'title': "Generated data", 'type': 'img', 'num_samples': 100, 'tile_shape': (10, 10), }, ]) model = WGAN(model_name="WGAN_CIFAR10", num_z=10, # set to 100 for a full run z_prior=Uniform1D(low=-1.0, high=1.0), img_size=(32, 32, 3), batch_size=16, # set to 64 for a full run num_conv_layers=3, # set to 3 for a full run num_gen_feature_maps=4, # set to 32 for a full run num_dis_feature_maps=4, # set to 32 for a full run metrics=['d_loss', 'g_loss'], callbacks=[loss_display, sample_display], num_epochs=4, # set to 100 for a full run log_path=os.path.join(root_dir, "logs"), random_state=random_seed(), verbose=1) model.fit(x_train)
def test_image_saver_callback(): np.random.seed(random_seed()) (x_train, y_train), (_, _) = demo.load_mnist() (cifar10_train, _), (_, _) = demo.load_cifar10() imgsaver1 = ImageSaver(freq=1, filepath=os.path.join( model_dir(), "male/callbacks/imagesaver/" "mnist/mnist_{epoch:04d}.png"), monitor={ 'metrics': 'x_data', 'img_size': (28, 28, 1), 'tile_shape': (10, 10), 'images': x_train[:100].reshape([-1, 28, 28, 1]) }) imgsaver2 = ImageSaver(freq=1, filepath=os.path.join( model_dir(), "male/callbacks/imagesaver/" "cifar10/cifar10_{epoch:04d}.png"), monitor={ 'metrics': 'x_data', 'img_size': (32, 32, 3), 'tile_shape': (10, 10), 'images': cifar10_train[:100].reshape([-1, 32, 32, 3]) }) optz = SGD(learning_rate=0.001) clf = GLM(model_name="imagesaver_callback", link='softmax', loss='softmax', optimizer=optz, num_epochs=4, batch_size=100, task='classification', callbacks=[imgsaver1, imgsaver2], random_state=random_seed(), verbose=1) clf.fit(x_train, y_train)
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_checkpoint(): 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]) filepath = os.path.join( model_dir(), "male/glm/checkpoint_{epoch:04d}_{val_loss:.6f}.pkl") checkpoint = ModelCheckpoint(filepath, mode='min', monitor='val_loss', verbose=1, save_best_only=True) optz = SGD(learning_rate=0.01) clf = GLM(model_name="checkpoint_callback", link='softmax', loss='softmax', optimizer=optz, num_epochs=5, batch_size=10, task='classification', metrics=['loss', 'err'], callbacks=[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) model_filepath = filepath.format(epoch=5, val_loss=0.968786) print("Load model at checkpoint: ", model_filepath, ", and predict:") clf1 = Model.load_model(model_filepath) train_err = 1.0 - clf1.score(x_train, y_train) test_err = 1.0 - clf1.score(x_test, y_test) print("Training error = %.4f" % train_err) print("Testing error = %.4f" % test_err)
def test_wgan_gp_cifar10_fid(show_figure=False, block_figure_on_end=False): print("========== Test WGAN-GP with FID on CIFAR10 data ==========") np.random.seed(random_seed()) num_data = 128 (x_train, y_train), (x_test, y_test) = demo.load_cifar10() x_train = x_train[:num_data].astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1. x_test = x_test.astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1. root_dir = os.path.join(model_dir(), "male/WGAN-GP/CIFAR10") checkpoints = ModelCheckpoint( os.path.join(root_dir, "checkpoints/{epoch:04d}_{FID:.6f}.ckpt"), mode='min', monitor='FID', verbose=1, save_best_only=True) loss_display = Display(layout=(1, 1), dpi='auto', show=show_figure, block_on_end=block_figure_on_end, filepath=[os.path.join(root_dir, "loss/loss_{epoch:04d}.png"), os.path.join(root_dir, "loss/loss_{epoch:04d}.pdf")], monitor=[{'metrics': ['d_loss', 'g_loss'], 'type': 'line', 'labels': ["discriminator loss", "generator loss"], 'title': "Losses", 'xlabel': "epoch", 'ylabel': "loss", }, ]) fid_display = Display(layout=(1, 1), dpi='auto', show=show_figure, block_on_end=block_figure_on_end, filepath=[os.path.join(root_dir, "FID/" "FID_{epoch:04d}.png"), os.path.join(root_dir, "FID/" "FID_{epoch:04d}.pdf")], monitor=[{'metrics': ['FID'], 'type': 'line', 'labels': ["FID"], 'title': "Scores", 'xlabel': "epoch", 'ylabel': "score", }, ], ) sample_display = Display(layout=(1, 1), dpi='auto', figsize=(10, 10), freq=1, show=show_figure, block_on_end=block_figure_on_end, filepath=os.path.join(root_dir, "samples/" "samples_{epoch:04d}.png"), monitor=[{'metrics': ['x_samples'], 'title': "Generated data", 'type': 'img', 'num_samples': 100, 'tile_shape': (10, 10), }, ]) model = WGAN_GP(model_name="WGAN_GP_CIFAR10", num_z=10, # set to 100 for a full run img_size=(32, 32, 3), batch_size=16, # set to 64 for a full run num_conv_layers=3, # set to 3 for a full run num_gen_feature_maps=4, # set to 32 for a full run num_dis_feature_maps=4, # set to 32 for a full run metrics=['d_loss', 'g_loss', 'FID', 'FID_100points'], callbacks=[loss_display, fid_display, sample_display, checkpoints], num_epochs=4, # set to 100 for a full run inception_metrics=[FID(data="cifar10"), FID(name="FID_100points", data=x_train[:100])], inception_metrics_freq=1, # summary_freq=1, # uncomment this for a full run log_path=os.path.join(root_dir, "logs"), random_state=random_seed(), verbose=1) model.fit(x_train)
def test_wgan_gp_mnist(show_figure=False, block_figure_on_end=False): print("========== Test WGAN-GP on MNIST data ==========") np.random.seed(random_seed()) (x_train, y_train), (x_test, y_test) = demo.load_mnist() x_train = x_train.astype(np.float32).reshape([-1, 28, 28, 1]) / 0.5 - 1. x_test = x_test.astype(np.float32).reshape([-1, 28, 28, 1]) / 0.5 - 1. root_dir = os.path.join(model_dir(), "male/WGAN-GP/MNIST") loss_display = Display(layout=(1, 1), dpi='auto', show=show_figure, block_on_end=block_figure_on_end, filepath=[os.path.join(root_dir, "loss/loss_{epoch:04d}.png"), os.path.join(root_dir, "loss/loss_{epoch:04d}.pdf")], monitor=[{'metrics': ['d_loss', 'g_loss'], 'type': 'line', 'labels': ["discriminator loss", "generator loss"], 'title': "Losses", 'xlabel': "epoch", 'ylabel': "loss", }, ]) sample_display = Display(layout=(1, 1), dpi='auto', figsize=(10, 10), freq=1, show=show_figure, block_on_end=block_figure_on_end, monitor=[{'metrics': ['x_samples'], 'title': "Generated data", 'type': 'img', 'num_samples': 100, 'tile_shape': (10, 10), }, ]) model = WGAN_GP(model_name="WGAN_GP_MNIST_z_uniform", num_z=10, # set to 100 for a full run z_prior=Uniform1D(low=-1.0, high=1.0), img_size=(28, 28, 1), batch_size=16, # set to 64 for a full run num_conv_layers=3, # set to 3 for a full run num_gen_feature_maps=4, # set to 32 for a full run num_dis_feature_maps=4, # set to 32 for a full run metrics=['d_loss', 'g_loss'], callbacks=[loss_display, sample_display], num_epochs=4, # set to 100 for a full run # summary_freq=1, # uncomment this for a full run random_state=random_seed(), log_path=os.path.join(root_dir, "logs"), verbose=1) model.fit(x_train) model = WGAN_GP(model_name="WGAN_GP_MNIST_z_Gaussian", num_z=10, # set to 100 for a full run z_prior=Gaussian1D(mu=0.0, sigma=1.0), img_size=(28, 28, 1), batch_size=32, # set to 64 for a full run num_conv_layers=3, # set to 3 for a full run num_gen_feature_maps=4, # set to 32 for a full run num_dis_feature_maps=4, # set to 32 for a full run metrics=['d_loss', 'g_loss'], callbacks=[loss_display, sample_display], num_epochs=4, # set to 100 for a full run # summary_freq=1, # uncomment this for a full run random_state=random_seed(), log_path=os.path.join(root_dir, "logs"), verbose=1) model.fit(x_train)
def test_pytorch_mlp_v1(show=False, block_figure_on_end=False): print("========== Test PytorchMLPv1 ==========") np.random.seed(random_seed()) (x_train, y_train), (x_test, y_test) = demo.load_mnist() idx_train = np.random.permutation(x_train.shape[0]) x_train = x_train[idx_train].astype(np.float32) y_train = y_train[idx_train].astype(np.uint8) print("Number of training samples = {}".format(x_train.shape[0])) idx_test = np.random.permutation(x_test.shape[0]) x_test = x_test[idx_test].astype(np.float32) y_test = y_test[idx_test].astype(np.uint8) print("Number of testing samples = {}".format(x_test.shape[0])) x = np.vstack([x_train, x_test]) y = np.concatenate([y_train, y_test]) err_display = Display(title="Error curves", dpi='auto', layout=(1, 1), freq=1, show=show, block_on_end=block_figure_on_end, monitor=[{ 'metrics': ['err', 'val_err'], 'type': 'line', 'title': "Learning errors", 'xlabel': "epoch", 'ylabel': "error", }]) loss_display = Display( title="Learning curves", dpi='auto', layout=(3, 1), freq=1, show=show, block_on_end=block_figure_on_end, filepath=[ os.path.join(model_dir(), "male/PyTorchMLP/" "loss/loss_{epoch:04d}.png"), os.path.join(model_dir(), "male/PyTorchMLP/" "loss/loss_{epoch:04d}.pdf") ], monitor=[ { 'metrics': ['loss', 'val_loss'], 'type': 'line', 'labels': ["training loss", "validation loss"], 'title': "Learning losses", 'xlabel': "epoch", 'xlabel_params': { 'fontsize': 50 }, '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", }, ]) weight_display = Display(title="Filters", dpi='auto', layout=(1, 1), figsize=(6, 15), freq=1, show=show, block_on_end=block_figure_on_end, filepath=os.path.join( model_dir(), "male/PyTorchMLP/" "weights/weights_{epoch:04d}.png"), monitor=[ { 'metrics': ['weights'], 'title': "Learned weights", 'type': 'img', 'tile_shape': (5, 2), }, ]) clf = PyTorchMLP(model_name='PyTorchMLP', arch='MLPv1', num_epochs=4, batch_size=100, metrics=['loss', 'err'], callbacks=[loss_display, err_display, weight_display], cv=[-1] * x_train.shape[0] + [0] * x_test.shape[0], random_state=random_seed(), verbose=1) clf.fit(x, y) print("Training error = %.4f" % (1.0 - clf.score(x_train, y_train))) print("Testing error = %.4f" % (1.0 - clf.score(x_test, y_test)))
def test_glm_save_load(show=False, block_figure_on_end=False): print("========== Test Save and Load functions for GLM ==========") 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=5, verbose=1) filepath = os.path.join(model_dir(), "male/GLM/iris_{epoch:04d}_{val_err:.6f}.pkl") checkpoint = ModelCheckpoint(filepath, mode='min', monitor='val_err', verbose=0, save_best_only=True) loss_display = Display(title="Learning curves", dpi='auto', layout=(3, 1), freq=1, 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", }, ]) weight_display = Display(title="Filters", dpi='auto', layout=(1, 1), figsize=(6, 15), freq=1, show=show, block_on_end=block_figure_on_end, monitor=[ { 'metrics': ['weights'], 'title': "Learned weights", 'type': 'img', 'disp_dim': (2, 2), 'tile_shape': (3, 1), }, ]) clf = GLM( model_name="GLM_softmax_cv", link='softmax', loss='softmax', optimizer='sgd', num_epochs=4, batch_size=10, task='classification', metrics=['loss', 'err'], callbacks=[early_stopping, checkpoint, loss_display, weight_display], 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) save_file_path = os.path.join(model_dir(), "male/GLM/saved_model.pkl") clf.save(file_path=save_file_path) clf1 = Model.load_model(save_file_path) clf1.num_epochs = 10 clf1.fit(x, y) train_err = 1.0 - clf1.score(x_train, y_train) test_err = 1.0 - clf1.score(x_test, y_test) print("Training error = %.4f" % train_err) print("Testing error = %.4f" % test_err)
def test_gank_image_saver(): print("========== Test GANK-Logit with Image Saver ==========") np.random.seed(random_seed()) (x_train, y_train), (x_test, y_test) = demo.load_mnist() x_train = x_train.astype(np.float32).reshape([-1, 28, 28, 1]) / 0.5 - 1. x_test = x_test.astype(np.float32).reshape([-1, 28, 28, 1]) / 0.5 - 1. imgsaver = ImageSaver(freq=1, filepath=os.path.join( model_dir(), "male/GANK/imagesaver/" "mnist/mnist_{epoch:04d}.png"), monitor={ 'metrics': 'x_samples', 'num_samples': 100, 'tile_shape': (10, 10), }) model = GANK( model_name="GANK_MNIST", num_random_features=50, # set to 1000 for a full run gamma_init=0.01, loss='logit', num_z=10, # set to 100 for a full run img_size=(28, 28, 1), batch_size=32, # set to 64 for a full run num_conv_layers=3, # set to 3 for a full run num_gen_feature_maps=4, # set to 32 for a full run num_dis_feature_maps=4, # set to 32 for a full run metrics=['d_loss', 'g_loss'], callbacks=[imgsaver], num_epochs=4, # set to 100 for a full run random_state=random_seed(), verbose=1) model.fit(x_train) np.random.seed(random_seed()) (x_train, y_train), (x_test, y_test) = demo.load_cifar10() x_train = x_train.astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1. x_test = x_test.astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1. imgsaver = ImageSaver(freq=1, filepath=os.path.join( model_dir(), "male/GANK/imagesaver/" "cifar10/cifar10_{epoch:04d}.png"), monitor={ 'metrics': 'x_samples', 'num_samples': 100, 'tile_shape': (10, 10), }) model = GANK( model_name="GANK_CIFAR10", num_random_features=50, # set 1000 for a full run gamma_init=0.01, loss='logit', num_z=10, # set to 100 for a full run img_size=(32, 32, 3), batch_size=32, # set to 64 for a full run num_conv_layers=3, # set to 3 for a full run num_gen_feature_maps=4, # set to 32 for a full run num_dis_feature_maps=4, # set to 32 for a full run metrics=['d_loss', 'g_loss'], callbacks=[imgsaver], num_epochs=4, # set to 500 for a full run random_state=random_seed(), verbose=1) model.fit(x_train)
def test_gank_logit_cifar10_inception_score(show_figure=False, block_figure_on_end=False): print( "========== Test GANK-Logit with Inception Score on CIFAR10 data ==========" ) np.random.seed(random_seed()) (x_train, y_train), (x_test, y_test) = demo.load_cifar10() x_train = x_train.astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1. x_test = x_test.astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1. filepath = os.path.join( model_dir(), "male/GANK/Logit/cifar10/checkpoints/" "{epoch:04d}_{inception_score:.6f}.ckpt") checkpoint = ModelCheckpoint(filepath, mode='max', monitor='inception_score', verbose=0, save_best_only=True) loss_display = Display(layout=(1, 1), dpi='auto', show=show_figure, block_on_end=block_figure_on_end, filepath=[ os.path.join( model_dir(), "male/GANK/Logit/cifar10/" "loss/loss_{epoch:04d}.png"), os.path.join( model_dir(), "male/GANK/Logit/cifar10/" "loss/loss_{epoch:04d}.pdf") ], monitor=[ { 'metrics': ['d_loss', 'g_loss'], 'type': 'line', 'labels': ["discriminator loss", "generator loss"], 'title': "Losses", 'xlabel': "epoch", 'ylabel': "loss", }, ]) inception_score_display = Display( layout=(1, 1), dpi='auto', show=show_figure, block_on_end=block_figure_on_end, filepath=[ os.path.join( model_dir(), "male/GANK/Logit/cifar10/inception_score/" "inception_score_{epoch:04d}.png"), os.path.join( model_dir(), "male/GANK/Logit/cifar10/inception_score/" "inception_score_{epoch:04d}.pdf") ], monitor=[ { 'metrics': ['inception_score'], 'type': 'line', 'labels': ["Inception Score"], 'title': "Scores", 'xlabel': "epoch", 'ylabel': "score", }, ], ) sample_display = Display(layout=(1, 1), dpi='auto', figsize=(10, 10), freq=1, show=show_figure, block_on_end=block_figure_on_end, filepath=os.path.join( model_dir(), "male/GANK/Logit/cifar10/samples/" "samples_{epoch:04d}.png"), monitor=[ { 'metrics': ['x_samples'], 'title': "Generated data", 'type': 'img', 'num_samples': 100, 'tile_shape': (10, 10), }, ]) model = GANK( model_name="GANK-Logit_CIFAR10", num_random_features=50, # set 1000 for a full run gamma_init=0.01, loss='logit', num_z=10, # set to 100 for a full run img_size=(32, 32, 3), batch_size=32, # set to 64 for a full run num_conv_layers=3, # set to 3 for a full run num_gen_feature_maps=4, # set to 32 for a full run num_dis_feature_maps=4, # set to 32 for a full run metrics=['d_loss', 'g_loss', 'inception_score'], # callbacks=[loss_display, inception_score_display, sample_display, checkpoint], callbacks=[checkpoint], num_epochs=4, # set to 500 for a full run inception_score_freq=1, random_state=random_seed(), verbose=1) model.fit(x_train)
def test_srbm_regression(show_figure=False, block_figure_on_end=False): print("========== Test Supervised RBM for Regression ==========") from sklearn.metrics import mean_squared_error from sklearn.linear_model import LinearRegression np.random.seed(random_seed()) (x_train, y_train), (x_test, y_test) = demo.load_mnist() x = np.vstack([x_train, x_test]) y = np.concatenate([y_train, y_test]) learning_display = Display( title="Learning curves", dpi='auto', layout=(3, 1), freq=1, show=show_figure, block_on_end=block_figure_on_end, monitor=[ { 'metrics': ['recon_err', 'val_recon_err'], 'type': 'line', 'labels': ["training recon error", "validation recon error"], 'title': "Reconstruction Errors", 'xlabel': "epoch", 'ylabel': "error", }, { 'metrics': ['loss', 'val_loss'], 'type': 'line', 'labels': ["training loss", "validation loss"], 'title': "Learning Losses", 'xlabel': "epoch", 'ylabel': "loss", }, { 'metrics': ['err', 'val_err'], 'type': 'line', 'labels': ["training error", "validation error"], 'title': "Prediction Errors", 'xlabel': "epoch", 'ylabel': "error", }, # {'metrics': ['loglik_csl', 'val_loglik_csl'], # 'type': 'line', # 'labels': ["training loglik (CSL)", "validation loglik (CSL)"], # 'title': "Loglikelihoods using CSL", # 'xlabel': "epoch", # 'ylabel': "loglik", # }, ]) filter_display = Display(title="Receptive Fields", dpi='auto', layout=(1, 1), figsize=(8, 8), freq=1, show=show_figure, block_on_end=block_figure_on_end, monitor=[ { 'metrics': ['filters'], 'title': "Receptive Fields", 'type': 'img', 'num_filters': 15, 'disp_dim': (28, 28), 'tile_shape': (3, 5), }, ]) hidden_display = Display(title="Hidden Activations", dpi='auto', layout=(1, 1), figsize=(8, 8), freq=1, show=show_figure, block_on_end=block_figure_on_end, monitor=[ { 'metrics': ['hidden_activations'], 'title': "Hidden Activations", 'type': 'img', 'data': x_train[:100], }, ]) early_stopping = EarlyStopping(monitor='val_loss', patience=2, verbose=1) filepath = os.path.join(model_dir(), "male/sRBM/mnist_{epoch:04d}_{val_loss:.6f}.pkl") checkpoint = ModelCheckpoint(filepath, mode='min', monitor='val_loss', verbose=0, save_best_only=True) model = SupervisedRBM(task='regression', num_hidden=15, num_visible=784, batch_size=100, num_epochs=4, w_init=0.01, learning_rate=0.01, momentum_method='sudden', weight_cost=0.0, inference_engine='variational_inference', approx_method='first_order', metrics=['recon_err', 'loss', 'err'], callbacks=[ filter_display, learning_display, hidden_display, early_stopping, checkpoint ], cv=[-1] * x_train.shape[0] + [0] * x_test.shape[0], random_state=random_seed(), verbose=1) model.fit(x, y) print("Test reconstruction error = %.4f" % model.get_reconstruction_error(x_test).mean()) print("Test loss = %.4f" % model.get_loss(x_test, y_test)) print("=========== Predicted by sRBM ============") print("Train MSE = {0:>1.4f}\tTest MSE = {1:>1.4f}".format( -model.score(x_train, y_train), -model.score(x_test, y_test))) # fit a Linear Regressor lr = LinearRegression() lr.fit(x_train, y_train) print("=========== Predicted by Linear Regressor ============") print("Train MSE = {0:>1.4f}\tTest MSE = {1:>1.4f}".format( mean_squared_error(y_train, lr.predict(x_train)), mean_squared_error(y_test, lr.predict(x_test))))
def test_display_callbacks(show=False, block_figure_on_end=False): np.random.seed(random_seed()) (x_train, y_train), (x_test, y_test) = demo.load_mnist() idx_train = np.random.permutation(x_train.shape[0]) x_train = x_train[idx_train] y_train = y_train[idx_train] print("Number of training samples = {}".format(x_train.shape[0])) idx_test = np.random.permutation(x_test.shape[0]) x_test = x_test[idx_test] y_test = y_test[idx_test] print("Number of testing samples = {}".format(x_test.shape[0])) x = np.vstack([x_train, x_test]) y = np.concatenate([y_train, y_test]) err_display = Display(title="Error curves", dpi='auto', layout=(1, 1), freq=1, show=show, block_on_end=block_figure_on_end, monitor=[{ 'metrics': ['err', 'val_err'], 'type': 'line', 'title': "Learning errors", 'xlabel': "epoch", 'ylabel': "error", }]) loss_display = Display( title="Learning curves", dpi='auto', layout=(3, 1), freq=1, show=show, block_on_end=block_figure_on_end, filepath=[ os.path.join(model_dir(), "male/callbacks/" "display/loss/loss_{epoch:04d}.png"), os.path.join(model_dir(), "male/callbacks/" "display/loss/loss_{epoch:04d}.pdf") ], monitor=[ { 'metrics': ['loss', 'val_loss'], 'type': 'line', 'labels': ["training loss", "validation loss"], 'title': "Learning losses", 'xlabel': "epoch", 'xlabel_params': { 'fontsize': 50 }, '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", }, ]) weight_display = Display(title="Filters", dpi='auto', layout=(1, 1), figsize=(6, 15), freq=1, show=show, block_on_end=block_figure_on_end, filepath=os.path.join( model_dir(), "male/callbacks/display/" "weights/weights_{epoch:04d}.png"), monitor=[ { 'metrics': ['weights'], 'title': "Learned weights", 'type': 'img', 'tile_shape': (5, 2), }, ]) optz = SGD(learning_rate=0.001) clf = GLM(model_name="display_callbacks", link='softmax', loss='softmax', optimizer=optz, num_epochs=20, batch_size=100, task='classification', metrics=['loss', 'err'], callbacks=[loss_display, weight_display, err_display], cv=[-1] * x_train.shape[0] + [0] * x_test.shape[0], random_state=random_seed(), verbose=1) clf.fit(x, y) print("Training error = %.4f" % (1.0 - clf.score(x_train, y_train))) print("Testing error = %.4f" % (1.0 - clf.score(x_test, y_test)))
def test_dcgan_image_saver(): print("========== Test DCGAN with Image Saver ==========") np.random.seed(random_seed()) num_data = 128 (x_train, y_train), (x_test, y_test) = demo.load_mnist() x_train = x_train[:num_data].astype(np.float32).reshape([-1, 28, 28, 1 ]) / 0.5 - 1. x_test = x_test.astype(np.float32).reshape([-1, 28, 28, 1]) / 0.5 - 1. root_dir = os.path.join(model_dir(), "male/DCGAN/imagesaver/mnist") imgsaver = ImageSaver(freq=1, filepath=os.path.join(root_dir, "mnist_{epoch:04d}.png"), monitor={ 'metrics': 'x_samples', 'num_samples': 100, 'tile_shape': (10, 10), }) model = DCGAN( model_name="DCGAN_MNIST", num_z=10, # set to 100 for a full run img_size=(28, 28, 1), batch_size=16, # set to 64 for a full run num_conv_layers=3, # set to 3 for a full run num_gen_feature_maps=4, # set to 32 for a full run num_dis_feature_maps=4, # set to 32 for a full run metrics=['d_loss', 'g_loss'], callbacks=[imgsaver], num_epochs=4, # set to 100 for a full run random_state=random_seed(), log_path=os.path.join(root_dir, "logs"), verbose=1) model.fit(x_train) np.random.seed(random_seed()) (x_train, y_train), (x_test, y_test) = demo.load_cifar10() x_train = x_train[:num_data].astype(np.float32).reshape([-1, 32, 32, 3 ]) / 0.5 - 1. x_test = x_test.astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1. root_dir = os.path.join(model_dir(), "male/DCGAN/imagesaver/cifar10") imgsaver = ImageSaver(freq=1, filepath=os.path.join(root_dir, "cifar10_{epoch:04d}.png"), monitor={ 'metrics': 'x_samples', 'num_samples': 100, 'tile_shape': (10, 10), }) model = DCGAN( model_name="DCGAN_CIFAR10", num_z=10, # set to 100 for a full run img_size=(32, 32, 3), batch_size=16, # set to 64 for a full run num_conv_layers=3, # set to 3 for a full run num_gen_feature_maps=4, # set to 32 for a full run num_dis_feature_maps=4, # set to 32 for a full run metrics=['d_loss', 'g_loss'], callbacks=[imgsaver], num_epochs=4, # set to 100 for a full run random_state=random_seed(), log_path=os.path.join(root_dir, "logs"), verbose=1) model.fit(x_train)
def test_ssrbm_classification(show_figure=False, block_figure_on_end=False): print("========== Test Semi-Supervised RBM for Classification ==========") num_labeled_data = 1000 from sklearn.metrics import accuracy_score from sklearn.neighbors import KNeighborsClassifier np.random.seed(random_seed()) (x_train, y_train), (x_test, y_test) = demo.load_mnist() # remove some labels idx_train, idx_test = next( iter( StratifiedShuffleSplit(n_splits=1, test_size=num_labeled_data, random_state=random_seed()).split( x_train, y_train))) y_train[idx_train] = 10**8 x = np.vstack([x_train, x_test]) y = np.concatenate([y_train, y_test]) learning_display = Display( title="Learning curves", dpi='auto', layout=(3, 1), freq=1, show=show_figure, block_on_end=block_figure_on_end, monitor=[ { 'metrics': ['recon_err', 'val_recon_err'], 'type': 'line', 'labels': ["training recon error", "validation recon error"], 'title': "Reconstruction Errors", 'xlabel': "epoch", 'ylabel': "error", }, { 'metrics': ['loss', 'val_loss'], 'type': 'line', 'labels': ["training loss", "validation loss"], 'title': "Learning Losses", 'xlabel': "epoch", 'ylabel': "loss", }, { 'metrics': ['err', 'val_err'], 'type': 'line', 'labels': ["training error", "validation error"], 'title': "Prediction Errors", 'xlabel': "epoch", 'ylabel': "error", }, # {'metrics': ['loglik_csl', 'val_loglik_csl'], # 'type': 'line', # 'labels': ["training loglik (CSL)", "validation loglik (CSL)"], # 'title': "Loglikelihoods using CSL", # 'xlabel': "epoch", # 'ylabel': "loglik", # }, ]) filter_display = Display(title="Receptive Fields", dpi='auto', layout=(1, 1), figsize=(8, 8), freq=1, show=show_figure, block_on_end=block_figure_on_end, monitor=[ { 'metrics': ['filters'], 'title': "Receptive Fields", 'type': 'img', 'num_filters': 15, 'disp_dim': (28, 28), 'tile_shape': (3, 5), }, ]) hidden_display = Display(title="Hidden Activations", dpi='auto', layout=(1, 1), figsize=(8, 8), freq=1, show=show_figure, block_on_end=block_figure_on_end, monitor=[ { 'metrics': ['hidden_activations'], 'title': "Hidden Activations", 'type': 'img', 'data': x_train[:100], }, ]) early_stopping = EarlyStopping(monitor='val_loss', patience=2, verbose=1) filepath = os.path.join(model_dir(), "male/ssRBM/mnist_{epoch:04d}_{val_loss:.6f}.pkl") checkpoint = ModelCheckpoint(filepath, mode='min', monitor='val_loss', verbose=0, save_best_only=True) model = SemiSupervisedRBM(num_hidden=15, num_visible=784, batch_size=100, num_epochs=4, learning_rate=0.1, w_init=0.1, momentum_method='sudden', weight_cost=0.0, inference_engine='variational_inference', approx_method='first_order', metrics=['recon_err', 'loss', 'err'], callbacks=[ filter_display, learning_display, hidden_display, early_stopping, checkpoint ], cv=[-1] * x_train.shape[0] + [0] * x_test.shape[0], random_state=random_seed(), verbose=1) model.fit(x, y) print("Test reconstruction error = %.4f" % model.get_reconstruction_error(x_test).mean()) print("=========== Predicted by Semi-Supervised RBM ============") print("Train accuracy = {0:>1.4f}\tTest accuracy = {1:>1.4f}".format( accuracy_score(y_train, model.predict(x_train)), accuracy_score(y_test, model.predict(x_test)))) x_train1 = model.transform(x_train) x_test1 = model.transform(x_test) clf = KNeighborsClassifier(n_neighbors=4) clf.fit(x_train1, y_train) print("=========== Predicted by kNN ============") print("Train accuracy = {0:>1.4f}\tTest accuracy = {1:>1.4f}".format( accuracy_score(y_train, clf.predict(x_train1)), accuracy_score(y_test, clf.predict(x_test1))))
def test_mggan2d_gmm2d(show_figure=False, block_figure_on_end=False): loss_display = Display( layout=(4, 1), dpi='auto', title='Loss', freq=1, # set to 1000 for a full run show=show_figure, block_on_end=block_figure_on_end, monitor=[ { 'metrics': ['d_loss', 'g_loss'], 'type': 'line', 'labels': ["discriminator loss", "generator loss"], 'title': "Losses", 'xlabel': "epoch", 'ylabel': "loss", }, { 'metrics': ['loglik'], 'type': 'line', 'labels': ["Log-likelihood"], 'title': "Evaluation", 'xlabel': "epoch", 'ylabel': "loglik", }, { 'metrics': ['d_bin_loss', 'd_mul_loss'], 'type': 'line', 'labels': ["d_bin_loss", "d_mul_loss"], 'title': "Discriminator Losses", 'xlabel': "epoch", 'ylabel': "loss", }, { 'metrics': ['g_bin_loss', 'g_mul_loss'], 'type': 'line', 'labels': ["g_bin_loss", "g_mul_loss"], 'title': "Generator Losses", 'xlabel': "epoch", 'ylabel': "loss", }, ], ) scatter_display = Display( layout=(1, 1), figsize=(12, 12), dpi='auto', filepath=[ os.path.join(model_dir(), "male/MGGAN2D/samples/" "scatter_{epoch:04d}.png"), os.path.join(model_dir(), "male/MGGAN2D/samples/" "scatter_{epoch:04d}.pdf") ], freq=1, # set to 1000 for a full run title='Scatter', show=show_figure, block_on_end=block_figure_on_end, monitor=[ { 'metrics': ['scatter'], 'type': 'scatter', 'title': "Data scatter generated by MGGAN2D", 'num_samples': 512, 'xlim': (-3, 3), 'ylim': (-3, 3), }, ]) num_mixtures = 8 radius = 2.0 std = 0.02 import numpy as np thetas = np.linspace(0, 2 * np.pi, num_mixtures + 1)[:num_mixtures] xs, ys = radius * np.sin(thetas), radius * np.cos(thetas) model = MGGAN2D( data=GMM(mix_coeffs=[1 / num_mixtures] * num_mixtures, mean=list(zip(xs, ys)), cov=[[std, std]] * num_mixtures), beta=0.05, # weight for g_bin_loss num_z=4, # set to 128 for a full run generator=Gaussian1D(mu=0.0, sigma=1.0), num_gens=4, d_batch_size=4, # set to 512 for full run g_batch_size=4, # set to 128 for full run num_epochs=4, # set to 25000 for a full run d_hidden_size=4, # set to 128 for a full run g_hidden_size=4, # set to 128 for a full run loglik_freq=1, generator_learning_rate=0.0002, discriminator_learning_rate=0.0002, summary_freq=250, metrics=[ 'd_bin_loss', 'd_mul_loss', 'd_loss', 'g_bin_loss', 'g_mul_loss', 'g_loss', 'loglik' ], callbacks=[loss_display, scatter_display], random_state=random_seed(), verbose=1) model.fit()
def test_wgan_gp_resnet_cifar10(show_figure=False, block_figure_on_end=False): print("========== Test WGAN-GP-ResNet on CIFAR10 data ==========") np.random.seed(random_seed()) num_data = 128 (x_train, y_train), (x_test, y_test) = demo.load_cifar10() x_train = x_train[:num_data].astype(np.float32).reshape([-1, 32, 32, 3 ]) / 0.5 - 1. x_test = x_test.astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1. root_dir = os.path.join(model_dir(), "male/WGAN-GP-ResNet/CIFAR10") loss_display = Display(layout=(1, 1), dpi='auto', show=show_figure, block_on_end=block_figure_on_end, filepath=[ os.path.join(root_dir, "loss/loss_{epoch:04d}.png"), os.path.join(root_dir, "loss/loss_{epoch:04d}.pdf") ], monitor=[ { 'metrics': ['d_loss', 'g_loss'], 'type': 'line', 'labels': ["discriminator loss", "generator loss"], 'title': "Losses", 'xlabel': "epoch", 'ylabel': "loss", }, ]) sample_display = Display(layout=(1, 1), dpi='auto', figsize=(10, 10), freq=1, show=show_figure, block_on_end=block_figure_on_end, monitor=[ { 'metrics': ['x_samples'], 'title': "Generated data", 'type': 'img', 'num_samples': 100, 'tile_shape': (10, 10), }, ]) model = WGAN_GP_ResNet( model_name="WGAN_GP_ResNet_CIFAR10", num_z=10, # set to 100 for a full run img_size=(32, 32, 3), batch_size=16, # set to 64 for a full run g_blocks=('up', 'up', 'up'), d_blocks=('down', 'down', None, None), num_gen_feature_maps=4, # set to 32 for a full run num_dis_feature_maps=4, # set to 32 for a full run metrics=['d_loss', 'g_loss'], callbacks=[loss_display, sample_display], num_epochs=2, # set to 100 for a full run random_state=random_seed(), log_path=os.path.join(root_dir, "logs"), verbose=1) model.fit(x_train) print("Saving model...") save_file_path = model.save(os.path.join(root_dir, "checkpoints/ckpt")) print("Reloading model...") model1 = TensorFlowModel.load_model(save_file_path) model1.num_epochs = 4 model1.fit(x_train) print("Done!")
def test_wgan_gp_resnet_cifar10_inception_metric(show_figure=False, block_figure_on_end=False): print( "========== Test WGAN-GP-ResNet with Inception Score and FID on CIFAR10 data ==========" ) np.random.seed(random_seed()) num_data = 128 (x_train, y_train), (x_test, y_test) = demo.load_cifar10() x_train = x_train[:num_data].astype(np.float32).reshape([-1, 32, 32, 3 ]) / 0.5 - 1. x_test = x_test.astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1. # uncomment for full run ''' import pickle from male.configs import data_dir tmp = pickle.load(open(os.path.join(data_dir(), "cifar10/cifar10_train.pkl"), "rb")) x_train = tmp['data'].astype(np.float32).reshape( [-1, 32, 32, 3]) / 127.5 - 1. ''' root_dir = os.path.join(model_dir(), "male/WGAN-GP-ResNet/CIFAR10") checkpoints_is = ModelCheckpoint(os.path.join( root_dir, "checkpoints_is/the_best_is.ckpt"), mode='max', monitor='inception_score', verbose=1, save_best_only=True) checkpoints_fid = ModelCheckpoint(os.path.join( root_dir, "checkpoints_fid/the_best_fid.ckpt"), mode='min', monitor='FID', verbose=1, save_best_only=True) loss_display = Display(layout=(1, 1), dpi='auto', show=show_figure, block_on_end=block_figure_on_end, filepath=[ os.path.join(root_dir, "loss/loss_{epoch:04d}.png"), os.path.join(root_dir, "loss/loss_{epoch:04d}.pdf") ], monitor=[ { 'metrics': ['d_loss', 'g_loss'], 'type': 'line', 'labels': ["discriminator loss", "generator loss"], 'title': "Losses", 'xlabel': "epoch", 'ylabel': "loss", }, ]) inception_score_display = Display( layout=(1, 1), dpi='auto', show=show_figure, block_on_end=block_figure_on_end, filepath=[ os.path.join(root_dir, "inception_score/" "inception_score_{epoch:04d}.png"), os.path.join(root_dir, "inception_score/" "inception_score_{epoch:04d}.pdf") ], monitor=[ { 'metrics': ['inception_score'], 'type': 'line', 'labels': ["Inception Score"], 'title': "Scores", 'xlabel': "epoch", 'ylabel': "score", }, ], ) fid_display = Display( layout=(1, 1), dpi='auto', show=show_figure, block_on_end=block_figure_on_end, filepath=[ os.path.join(root_dir, "FID/" "FID_{epoch:04d}.png"), os.path.join(root_dir, "FID/" "FID_{epoch:04d}.pdf") ], monitor=[ { 'metrics': ['FID'], 'type': 'line', 'labels': ["FID"], 'title': "Scores", 'xlabel': "epoch", 'ylabel': "score", }, ], ) sample_display = Display(layout=(1, 1), dpi='auto', figsize=(10, 10), freq=1, show=show_figure, block_on_end=block_figure_on_end, filepath=os.path.join( root_dir, "samples/samples_{epoch:04d}.png"), monitor=[ { 'metrics': ['x_samples'], 'title': "Generated data", 'type': 'img', 'num_samples': 100, 'tile_shape': (10, 10), }, ]) model = WGAN_GP_ResNet( model_name="WGAN_GP_ResNet_CIFAR10", num_z=8, # set to 128 for a full run img_size=(32, 32, 3), batch_size=64, # set to 64 for a full run g_blocks=('up', 'up', 'up'), d_blocks=('down', 'down', None, None), num_gen_feature_maps=8, # set to 128 for a full run num_dis_feature_maps=8, # set to 128 for a full run metrics=[ 'd_loss', 'g_loss', 'inception_score', 'inception_score_std', 'FID' ], callbacks=[ loss_display, inception_score_display, fid_display, sample_display, checkpoints_is, checkpoints_fid ], num_epochs=2, # set to 500 for a full run inception_metrics=[InceptionScore(), FID(data='cifar10')], inception_metrics_freq=1, num_inception_samples=100, # set to 50000 for a full run # summary_freq=1, # uncomment this for a full run log_path=os.path.join(root_dir, 'logs'), random_state=random_seed(), verbose=1) model.fit(x_train) filepath = os.path.join(root_dir, 'checkpoints_fid/the_best_fid.ckpt') print('Reloading the latest model at: {}'.format(filepath)) model1 = TensorFlowModel.load_model(filepath) model1.inception_metrics = InceptionMetricList( [InceptionScore(), FID(data='cifar10')]) model1.num_epochs = 4 model1.fit(x_train) print('Done!')
def test_dcgan_save_and_load(show_figure=False, block_figure_on_end=False): print( "========== Test Save and Load functions of DCGAN on MNIST data ==========" ) np.random.seed(random_seed()) (x_train, y_train), (x_test, y_test) = demo.load_mnist() x_train = x_train.astype(np.float32).reshape([-1, 28, 28, 1]) / 0.5 - 1. x_test = x_test.astype(np.float32).reshape([-1, 28, 28, 1]) / 0.5 - 1. root_dir = os.path.join(model_dir(), "male/DCGAN/MNIST") loss_display = Display(layout=(1, 1), dpi='auto', show=show_figure, block_on_end=block_figure_on_end, monitor=[ { 'metrics': ['d_loss', 'g_loss'], 'type': 'line', 'labels': ["discriminator loss", "generator loss"], 'title': "Losses", 'xlabel': "epoch", 'ylabel': "loss", }, ]) sample_display = Display(layout=(1, 1), dpi='auto', figsize=(10, 10), freq=1, show=show_figure, block_on_end=block_figure_on_end, monitor=[ { 'metrics': ['x_samples'], 'title': "Generated data", 'type': 'img', 'num_samples': 100, 'tile_shape': (10, 10), }, ]) model = DCGAN( model_name="DCGAN_MNIST_SaveLoad", num_z=10, # set to 100 for a full run img_size=(28, 28, 1), batch_size=16, # set to 64 for a full run num_conv_layers=3, # set to 3 for a full run num_gen_feature_maps=4, # set to 32 for a full run num_dis_feature_maps=4, # set to 32 for a full run metrics=['d_loss', 'g_loss'], callbacks=[loss_display, sample_display], num_epochs=2, # set to 100 for a full run log_path=os.path.join(root_dir, "logs"), random_state=random_seed(), verbose=1) model.fit(x_train) print("Saving model...") save_file_path = model.save(os.path.join(root_dir, "checkpoints/ckpt")) print("Reloading model...") model1 = TensorFlowModel.load_model(save_file_path) model1.num_epochs = 4 model1.fit(x_train) print("Done!")
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_dcgan_cifar10_inception_metric(show_figure=False, block_figure_on_end=False): print( "========== Test DCGAN with Inception Score and FID on CIFAR10 data ==========" ) np.random.seed(random_seed()) num_data = 128 (x_train, y_train), (x_test, y_test) = demo.load_cifar10() x_train = x_train[:num_data].astype(np.float32).reshape([-1, 32, 32, 3 ]) / 0.5 - 1. x_test = x_test.astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1. root_dir = os.path.join(model_dir(), "male/DCGAN/cifar10") checkpoints_is = ModelCheckpoint(os.path.join( root_dir, "checkpoints_is/the_best_is.ckpt"), mode='max', monitor='inception_score', verbose=1, save_best_only=True) checkpoints_fid = ModelCheckpoint(os.path.join( root_dir, "checkpoints_fid/the_best_fid.ckpt"), mode='min', monitor='FID', verbose=1, save_best_only=True) loss_display = Display(layout=(1, 1), dpi='auto', show=show_figure, block_on_end=block_figure_on_end, filepath=[ os.path.join(root_dir, "loss/loss_{epoch:04d}.png"), os.path.join(root_dir, "loss/loss_{epoch:04d}.pdf") ], monitor=[ { 'metrics': ['d_loss', 'g_loss'], 'type': 'line', 'labels': ["discriminator loss", "generator loss"], 'title': "Losses", 'xlabel': "epoch", 'ylabel': "loss", }, ]) inception_score_display = Display( layout=(1, 1), dpi='auto', show=show_figure, block_on_end=block_figure_on_end, filepath=[ os.path.join(root_dir, "inception_score/" "inception_score_{epoch:04d}.png"), os.path.join(root_dir, "inception_score/" "inception_score_{epoch:04d}.pdf") ], monitor=[ { 'metrics': ['inception_score'], 'type': 'line', 'labels': ["Inception Score"], 'title': "Scores", 'xlabel': "epoch", 'ylabel': "score", }, ], ) fid_display = Display( layout=(1, 1), dpi='auto', show=show_figure, block_on_end=block_figure_on_end, filepath=[ os.path.join(root_dir, "FID/" "FID_{epoch:04d}.png"), os.path.join(root_dir, "FID/" "FID_{epoch:04d}.pdf") ], monitor=[ { 'metrics': ['FID'], 'type': 'line', 'labels': ["FID"], 'title': "Scores", 'xlabel': "epoch", 'ylabel': "score", }, ], ) sample_display = Display(layout=(1, 1), dpi='auto', figsize=(10, 10), freq=1, show=show_figure, block_on_end=block_figure_on_end, filepath=os.path.join( root_dir, "samples/samples_{epoch:04d}.png"), monitor=[ { 'metrics': ['x_samples'], 'title': "Generated data", 'type': 'img', 'num_samples': 100, 'tile_shape': (10, 10), }, ]) model = DCGAN( model_name="DCGAN_CIFAR10", num_z=10, # set to 100 for a full run img_size=(32, 32, 3), batch_size=16, # set to 64 for a full run num_conv_layers=3, # set to 3 for a full run num_gen_feature_maps=4, # set to 32 for a full run num_dis_feature_maps=4, # set to 32 for a full run metrics=[ 'd_loss', 'g_loss', 'inception_score', 'inception_score_std', 'FID' ], callbacks=[ loss_display, inception_score_display, fid_display, sample_display, checkpoints_is, checkpoints_fid ], num_epochs=4, # set to 100 for a full run inception_metrics=[InceptionScore(), FID(data="cifar10")], inception_metrics_freq=1, # summary_freq=1, # uncomment this for a full run log_path=os.path.join(root_dir, "logs"), random_state=random_seed(), verbose=1) model.fit(x_train) filepath = os.path.join(root_dir, "checkpoints_fid/the_best_fid.ckpt") print("Reloading the latest model at: {}".format(filepath)) model1 = TensorFlowModel.load_model(filepath) model1.inception_metrics = InceptionMetricList( [InceptionScore(), FID(data="cifar10")]) model1.num_epochs = 6 model1.fit(x_train) print("Done!")
def test_gan_save_and_load(show_figure=False, block_figure_on_end=False): print( "========== Test Save and Load functions of GAN on MNIST data ==========" ) np.random.seed(random_seed()) (x_train, y_train), (x_test, y_test) = demo.load_mnist() x_train = x_train.astype(np.float32).reshape([-1, 28, 28, 1]) / 0.5 - 1. x_test = x_test.astype(np.float32).reshape([-1, 28, 28, 1]) / 0.5 - 1. root_dir = os.path.join(model_dir(), "male/GAN/MNIST") loss_display = Display(layout=(1, 1), dpi='auto', show=show_figure, block_on_end=block_figure_on_end, monitor=[ { 'metrics': ['d_loss', 'g_loss'], 'type': 'line', 'labels': ["discriminator loss", "generator loss"], 'title': "Losses", 'xlabel': "epoch", 'ylabel': "loss", }, ]) sample_display = Display(layout=(1, 1), dpi='auto', figsize=(10, 10), freq=1, show=show_figure, block_on_end=block_figure_on_end, monitor=[ { 'metrics': ['x_samples'], 'title': "Generated data", 'type': 'img', 'num_samples': 100, 'tile_shape': (10, 10), }, ]) model = GAN(model_name="GAN_MNIST_SaveLoad", num_x=784, num_discriminator_hiddens=(16, ), discriminator_batchnorm=False, discriminator_act_funcs=('lrelu', ), discriminator_learning_rate=0.001, num_z=8, generator_distribution=Uniform(low=(-1.0, ) * 8, high=(1.0, ) * 8), generator_batchnorm=False, num_generator_hiddens=(16, ), generator_act_funcs=('lrelu', ), generator_out_func='sigmoid', generator_learning_rate=0.001, batch_size=32, metrics=['d_loss', 'g_loss'], callbacks=[loss_display, sample_display], num_epochs=2, log_path=os.path.join(root_dir, "logs"), random_state=random_seed(), verbose=1) model.fit(x_train) print("Saving model...") save_file_path = model.save(os.path.join(root_dir, "checkpoints/ckpt")) print("Reloading model...") model1 = TensorFlowModel.load_model(save_file_path) model1.num_epochs = 4 model1.fit(x_train) print("Done!")
def test_pytorch_convnet_v1(show=False, block_figure_on_end=False): print("========== Test PytorchConvNetv1 ==========") np.random.seed(random_seed()) err_display = Display(title="Error curves", dpi='auto', layout=(1, 1), freq=1, show=show, block_on_end=block_figure_on_end, monitor=[{ 'metrics': ['err'], 'type': 'line', 'title': "Learning errors", 'xlabel': "epoch", 'ylabel': "error", }]) loss_display = Display( title="Learning curves", dpi='auto', layout=(3, 1), freq=1, show=show, block_on_end=block_figure_on_end, filepath=[ os.path.join(model_dir(), "male/PyTorchConvNet/" "loss/loss_{epoch:04d}.png"), os.path.join(model_dir(), "male/PyTorchConvNet/" "loss/loss_{epoch:04d}.pdf") ], monitor=[ { 'metrics': ['loss'], 'type': 'line', 'labels': ["training loss"], 'title': "Learning losses", 'xlabel': "epoch", 'xlabel_params': { 'fontsize': 50 }, 'ylabel': "loss", }, { 'metrics': ['err'], 'type': 'line', 'title': "Learning errors", 'xlabel': "epoch", 'ylabel': "error", }, { 'metrics': ['err'], 'type': 'line', 'labels': ["training error"], 'title': "Learning errors", 'xlabel': "epoch", 'ylabel': "error", }, ]) weight_display = Display(title="Filters", dpi='auto', layout=(1, 1), figsize=(6, 15), freq=1, show=show, block_on_end=block_figure_on_end, filepath=os.path.join( model_dir(), "male/PyTorchConvNet/" "weights/weights_{epoch:04d}.png"), monitor=[ { 'metrics': ['weights'], 'title': "Learned weights", 'type': 'img', 'tile_shape': (5, 2), }, ]) # Construct the model. clf = PyTorchConvNet(model_name='PyTorchConvNet', num_epochs=1, batch_size=64, metrics=['loss', 'err'], callbacks=[loss_display, err_display], random_state=random_seed(), verbose=1) print('Show some of the training images.') if False: clf.num_epochs = 0 # Build the network as well as data loaders. clf.fit() import torchvision def imshow(img): img = img / 2 + 0.5 # unnormalize npimg = img.numpy() plt.imshow(np.transpose(npimg, (1, 2, 0))) plt.show(block=True) # get some random training images dataiter = iter(clf.trainloader) images, labels = dataiter.next() # show images imshow(torchvision.utils.make_grid(images)) # print labels print(' '.join('%5s' % clf.classes[labels[j]] for j in range(4))) clf.num_epochs = 1 clf.fit() print('Testing accuracy = %.4f' % (100 * clf.acc_test())) save_file_path = clf.save() clf = PyTorchModel.load_model(save_file_path) clf.num_epochs = 4 clf.fit() print('Testing accuracy = %.4f' % (100 * clf.acc_test()))
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)