Esempio n. 1
0
def test_HyperOpt_space_with_data():
    data_pack = DatasetPackLoader().load_dataset('titanic')
    clf_name = 'skMLPClf'
    clf_pack = ClassifierPack()

    train_set = data_pack['train']

    # train_set, valid_set = train_set.split()
    # train_Xs, train_Ys = train_set.full_batch(['Xs', 'Ys'])
    # valid_Xs, valid_Ys = valid_set.full_batch(['Xs', 'Ys'])
    # clf_pack.fit(train_Xs, train_Ys)
    # score = clf_pack.score(valid_Xs, valid_Ys)
    # pprint(score)

    clf = clf_pack[clf_name]
    space = clf.HyperOpt_space

    # data_pack = None
    opt = HyperOpt()

    # space.update({'args': ()})
    # space.update({'kwargs':})
    opt.fit_serial(fit_fn, space, 10, feed_args=(), feed_kwargs={'data_pack': data_pack.to_DummyDatasetPack()})
    pprint(opt.best_loss)
    pprint(opt.best_param)

    opt.fit_parallel(fit_fn, space, 10, feed_args=(), feed_kwargs={'data_pack': data_pack.to_DummyDatasetPack()})
    pprint(opt.best_loss)
    pprint(opt.best_param)
Esempio n. 2
0
def helper_wrapper_clf_common(clf):
    datapack = DatasetPackLoader().load_dataset("titanic")
    train_set = datapack['train']
    train_set, valid_set = train_set.split((7, 3))
    train_Xs, train_Ys = train_set.full_batch(['Xs', 'Ys'])
    valid_Xs, valid_Ys = valid_set.full_batch(['Xs', 'Ys'])
    sample_Xs, sample_Ys = valid_Xs[:2], valid_Ys[:2]

    clf.fit(train_Xs, train_Ys)

    try:
        predict = clf.predict(valid_Xs)
        print(f'predict {predict}')
    except BaseException as e:
        print(f'while {clf} predict {e}')

    try:
        proba = clf.predict_proba(valid_Xs)
        print(f'proba {proba}')
    except BaseException as e:
        print(f'while {clf} predict_proba {e}')

    try:
        score = clf.score(valid_Xs, valid_Ys)
        print(f'score {score}')
    except BaseException as e:
        print(f'while {clf} score {e}')

    try:
        score = clf.score_pack(valid_Xs, valid_Ys)
        print(f'score_pack {score}')
    except BaseException as e:
        print(f'while {clf} score_pack {e}')
def test_wrapperRandomizedSearchCV():
    data_pack = DatasetPackLoader().load_dataset('titanic')
    train_set = data_pack['train']

    train_Xs, train_Ys = train_set.full_batch(['Xs', 'Ys'])

    clf_pack = ClassifierPack()
    clf = clf_pack['skMLPClf']

    from scipy import stats

    tuning_distributions = {
        'activation': ['relu'],
        'alpha': stats.expon(scale=0.001),
        # 'hidden_layer_sizes': [(128, 128), (64, 64), ],
        'max_iter': [1000],
        'learning_rate': ['constant', 'invscaling', 'adaptive'],
        # 'learning_rate_init': stats.expon(scale=0.001),
        # 'tol': [0.0001],
    }
    # 'alpha': hp.loguniform('alpha', -4, 3),
    # 'learning_rate': hp.choice('learning_rate', ['constant', 'invscaling', 'adaptive']),

    param_distributions = tuning_distributions
    search = wrapperRandomizedSearchCV(clf, param_distributions, n_iter=10)
    search.fit(train_Xs, train_Ys)
    # pprint(search.best_estimator_)
    # pprint(search.best_params_)
    # pprint(search.best_index_)
    pprint(search.best_score_)
    # pprint(search.cv_results_)

    pass
Esempio n. 4
0
def load_dataset():
    datapack = DatasetPackLoader().load_dataset('titanic')
    train_set = datapack['train']
    test_set = datapack['test']
    train_set.shuffle()
    train_Xs, train_Ys = train_set.full_batch(['Xs', 'Ys'])
    sample_Xs, sample_Ys = train_Xs[:1], train_Ys[:2]
    return train_Xs, train_Ys
def test_titanic_pack():
    datapack = DatasetPackLoader().load_dataset("titanic")
    train_set = datapack['train']

    train_set, valid_set = train_set.split((7, 3))
    train_Xs, train_Ys = train_set.full_batch(['Xs', 'Ys'])
    valid_Xs, valid_Ys = valid_set.full_batch(['Xs', 'Ys'])
    sample_Xs, sample_Ys = valid_Xs[:2], valid_Ys[:2]
    print(train_Xs)
Esempio n. 6
0
def test_AAE_with_noise():
    class_ = AAE
    data_pack = DatasetPackLoader().load_dataset("MNIST")
    dataset = data_pack['train']
    Xs, Ys = dataset.full_batch(['Xs', 'Ys'])
    sample_X = Xs[:2]
    sample_Y = Ys[:2]

    model = class_(with_noise=True)
    AAE_total_execute(model)
Esempio n. 7
0
def test_MLPClf():
    class_ = MLPClassifier
    data_pack = DatasetPackLoader().load_dataset("titanic")
    dataset = data_pack['train']
    Xs, Ys = dataset.full_batch(['Xs', 'Ys'])
    sample_X = Xs[:2]
    sample_Y = Ys[:2]

    model = class_()
    model.train(Xs, Ys)

    predict = model.predict(sample_X)
    # print(predict)

    score = model.score(Xs, Ys)
    # print(score)

    proba = model.predict_proba(sample_X)
    # print(proba)

    metric = model.metric(sample_X, sample_Y)
    # print(metric)

    path = model.save()

    model = class_()
    model.load_meta(path)
    model.train(Xs, Ys)

    predict = model.predict(sample_X)
    # print(predict)

    score = model.score(Xs, Ys)
    # print(score)

    proba = model.predict_proba(sample_X)
    # print(proba)

    metric = model.metric(sample_X, sample_Y)
    # print(metric)

    model.save()
Esempio n. 8
0
def test_wrapperclfpack_HyperOpt_parallel():
    data_pack = DatasetPackLoader().load_dataset('titanic')
    clf_name = 'skMLPClf'
    # clf_pack = ClassifierPack(['skGaussian_NBClf', 'skMLPClf'])
    clf_pack = ClassifierPack()

    train_set = data_pack['train']
    train_set.shuffle()
    train_set, valid_set = train_set.split()
    train_Xs, train_Ys = train_set.full_batch(['Xs', 'Ys'])
    valid_Xs, valid_Ys = valid_set.full_batch(['Xs', 'Ys'])

    clf_pack.HyperOptSearch(train_Xs, train_Ys, n_iter=3, parallel=True)
    pprint(clf_pack.optimize_result)
    pprint(clf_pack.HyperOpt_best_loss)
    pprint(clf_pack.HyperOpt_best_params)
    pprint(clf_pack.HyperOpt_best_result)
    pprint(clf_pack.HyperOpt_opt_info)

    score = clf_pack.score_pack(valid_Xs, valid_Ys)
    pprint(score)
Esempio n. 9
0
def test_hyperOpt():
    data_pack = DatasetPackLoader().load_dataset('titanic')

    def fit_clf(kwargs):
        params = kwargs['params']
        data_pack = kwargs['data_pack']
        train_set = data_pack['train']
        train_set.shuffle()
        train_set, valid_set = train_set.split()
        train_Xs, train_Ys = train_set.full_batch(['Xs', 'Ys'])
        valid_Xs, valid_Ys = valid_set.full_batch(['Xs', 'Ys'])

        clf_pack = ClassifierPack()
        clf = clf_pack['skMLPClf']
        clf = clf.__class__(**params)
        clf.fit(train_Xs, train_Ys)
        score = -clf.score(valid_Xs, valid_Ys)

        return score

    space = hp.choice('classifier_type', [{
        'alpha': hp.loguniform('alpha', -4, 3),
        'learning_rate': hp.choice('learning_rate', ['constant', 'invscaling', 'adaptive']),
        'activation': hp.choice('activation', ['relu']),
        'max_iter': 1000,
    }])

    opt = HyperOpt()
    best = opt.fit_serial(fit_clf, data_pack, space, 10)
    pprint(best)
    pprint(opt.trials)
    pprint(opt.losses)
    pprint(opt.result)
    pprint(opt.opt_info)
    pprint(opt.best_param)
    pprint(opt.best_loss)
    pprint(opt.statuses)
Esempio n. 10
0
def load_dataset():
    datapack = DatasetPackLoader().load_dataset('titanic')
    train_set = datapack['train']
    train_set.shuffle()
    train_Xs, train_Ys = train_set.full_batch(['Xs', 'Ys'])
    return train_Xs
Esempio n. 11
0
def load_dataset(dataset_name):
    data_pack = DatasetPackLoader().load_dataset(dataset_name)
    train_set = data_pack['train']
    train_Xs, train_Ys = train_set.full_batch(['Xs', 'Ys'])
    return train_Xs, train_Ys
Esempio n. 12
0
from pprint import pprint

from script.sklearn_like_toolkit.warpper.skClf_wrapper.skBernoulli_NBClf import skBernoulli_NBClf
from script.sklearn_like_toolkit.param_search.HyperOpt.wrapperGridSearchCV import wrapperGridSearchCV
from script.data_handler.DatasetPackLoader import DatasetPackLoader
from script.sklearn_like_toolkit.ClassifierPack import ClassifierPack

meta_clf = skBernoulli_NBClf()
datapack = DatasetPackLoader().load_dataset("titanic")
train_set = datapack['train']
train_set, valid_set = train_set.split((7, 3))
train_Xs, train_Ys = train_set.full_batch(['Xs', 'Ys'])
valid_Xs, valid_Ys = valid_set.full_batch(['Xs', 'Ys'])
sample_Xs, sample_Ys = valid_Xs[:2], valid_Ys[:2]


def test_param_search():
    clf_pack = ClassifierPack(['skMLPClf'])
    clf_pack.param_search(train_Xs, train_Ys)
    train_score = clf_pack.score(train_Xs, train_Ys)
    valid_score = clf_pack.score(valid_Xs, valid_Ys)
    print(train_score)
    print(valid_score)

    path = clf_pack.save_params()
    clf_pack.load_params(path)
    clf_pack.fit(train_Xs, train_Ys)
    train_score = clf_pack.score(train_Xs, train_Ys)
    valid_score = clf_pack.score(valid_Xs, valid_Ys)
    print(train_score)
    print(valid_score)
Esempio n. 13
0
from pprint import pprint
from hyperopt import hp
from script.data_handler.DatasetPackLoader import DatasetPackLoader
from script.sklearn_like_toolkit.ClassifierPack import ClassifierPack
from script.sklearn_like_toolkit.param_search.HyperOpt.FreeTrials import FreeTrials
from script.sklearn_like_toolkit.param_search.HyperOpt.HyperOpt import HyperOpt, HyperOpt_fn
from script.util.deco import deco_timeit

data_pack = DatasetPackLoader().load_dataset('titanic')


def fit_clf(params):
    train_set = data_pack['train']
    train_set.shuffle()
    train_set, valid_set = train_set.split()
    train_Xs, train_Ys = train_set.full_batch(['Xs', 'Ys'])
    valid_Xs, valid_Ys = valid_set.full_batch(['Xs', 'Ys'])

    clf_pack = ClassifierPack()
    clf = clf_pack['skMLPClf']
    clf = clf.__class__(**params)
    clf.fit(train_Xs, train_Ys)
    score = -clf.score(valid_Xs, valid_Ys)

    return score


@deco_timeit
def test_hyperOpt():
    data_pack = DatasetPackLoader().load_dataset('titanic')
Esempio n. 14
0
import numpy as np
from script.data_handler.DatasetPackLoader import DatasetPackLoader
from script.data_handler.MNIST import MNIST
from script.model.sklearn_like_model.AE.VAE import VAE
from script.sklearn_like_toolkit.param_search.param_grid import param_grid_full
from script.util.PlotTools import PlotTools
from script.util.misc_util import params_to_dict

class_ = VAE
data_pack = DatasetPackLoader().load_dataset("MNIST")
dataset = data_pack['train']
Xs, Ys = dataset.full_batch()
sample_X = Xs[:2]


def VAE_total_execute(model):
    model.train(Xs, epoch=1)

    metric = model.metric(sample_X)
    print(metric)

    code = model.code(sample_X)
    print(code)

    recon = model.recon(sample_X)
    print(recon)

    path = model.save()

    model = class_()
    model.load_meta(path)