Example #1
0
    def test_run_grid(self):
        auto_var = AutoVar()
        auto_var.add_variable_class(OrdVarClass())
        auto_var.add_variable_class(DatasetVarClass())
        auto_var.add_variable('random_seed', int)

        grid_params = {
            "ord": ['1', '2'],
            "dataset": ['halfmoon_50', 'halfmoon_10'],
            "random_seed": [1126],
        }

        def fn(auto_var):
            return {
                "ord": auto_var.var_value['ord'],
                "dataset": auto_var.var_value['dataset'],
                "random_seed": auto_var.var_value['random_seed'],
            }

        params, results = auto_var.run_grid_params(fn,
                                                   grid_params=grid_params,
                                                   n_jobs=1)

        self.assertEqual(params[0], {
            'ord': '1',
            'dataset': 'halfmoon_50',
            'random_seed': 1126,
        })

        del results[0]['var_value']['git_hash']
        self.assertEqual(params[0], results[0]['var_value'])

        del results[0]['running_time']
        del results[0]['var_value']
        self.assertEqual(params[0], results[0])
Example #2
0
    def test_val(self):
        auto_var = AutoVar(logging_level=logging.INFO)
        with self.assertRaises(VariableNotRegisteredError):
            auto_var.get_var('ord')

        auto_var.add_variable_class(OrdVarClass())
        auto_var.add_variable_class(DatasetVarClass())
        auto_var.add_variable('random_seed', int)

        with self.assertRaises(VariableValueNotSetError):
            auto_var.get_var('ord')

        auto_var.set_variable_value_by_dict({
            'ord': '1',
            'dataset': 'halfmoon_200',
            'random_seed': 1126
        })

        self.assertEqual(auto_var.get_var('ord'), 1)
        self.assertEqual(auto_var.get_var('random_seed'), 1126)
        self.assertEqual(len(auto_var.get_var('dataset')[0]), 200)
        self.assertEqual(
            len(auto_var.get_var_with_argument('dataset', 'halfmoon_300')[0]),
            300)

        with self.assertRaises(ValueError):
            auto_var.set_variable_value_by_dict({'ord': 'l2'})

        with self.assertRaises(TypeError):
            auto_var.set_variable_value_by_dict({'random_seed': '1126.0'})

        self.assertEqual(auto_var.get_var_shown_name(var_name="dataset"),
                         'shown_halfmoon')

        assert_array_equal(
            auto_var.get_var_with_argument('dataset', 'halfmoon_300')[0],
            auto_var.get_var_with_argument('dataset', 'moon_300')[0],
        )
        argparse_help = auto_var.get_argparser().format_help()
        self.assertTrue('halfmoon dataset' in argparse_help)
        self.assertTrue('Dataset variable class' in argparse_help)
Example #3
0
                       partial(create_placeholder_file,
                               get_name_fn=get_file_name),
                   ],
                   after_experiment_hooks=[
                       partial(save_result_to_file, get_name_fn=get_file_name),
                       partial(remove_placeholder_if_error,
                               get_name_fn=get_file_name),
                   ],
                   settings={
                       'file_format': 'pickle',
                       'server_url': '',
                       'result_file_dir': './results/'
                   })

auto_var.add_variable_class(DatasetVarClass())
auto_var.add_variable('random_seed', int)
#auto_var.add_variable('eps', float)
#auto_var.add_variable('optimizer', str)
#auto_var.add_variable('learning_rate', float, default=1e-2)
#auto_var.add_variable('batch_size', int, default=64)
#auto_var.add_variable('momentum', float, default=0.9)
#auto_var.add_variable('epochs', int, default=2)
#auto_var.add_variable('weight_decay', float, default=0.)

#from autovar.base import RegisteringChoiceType, VariableClass, register_var
#class ExampleVarClass(VariableClass, metaclass=RegisteringChoiceType):
#    """Example Variable Class"""
#    var_name = "example"
#
#    @register_var()
#    @staticmethod
from autovar.base import RegisteringChoiceType, register_var, VariableClass
from autovar.hooks import check_result_file_exist, save_result_to_file
from autovar.hooks import create_placeholder_file, remove_placeholder_if_error
from autovar.hooks import default_get_file_name as get_file_name

from .datasets import DatasetVarClass
from .preprocessors import PreprocessorVarClass

auto_var = AutoVar(logging_level=logging.INFO,
                   before_experiment_hooks=[
                       partial(check_result_file_exist,
                               get_name_fn=get_file_name),
                       partial(create_placeholder_file,
                               get_name_fn=get_file_name),
                   ],
                   after_experiment_hooks=[
                       partial(save_result_to_file, get_name_fn=get_file_name),
                       partial(remove_placeholder_if_error,
                               get_name_fn=get_file_name),
                   ],
                   settings={
                       'file_format': 'pickle',
                       'server_url': '',
                       'result_file_dir': './results/'
                   })

auto_var.add_variable_class(DatasetVarClass())
auto_var.add_variable_class(PreprocessorVarClass())
auto_var.add_variable('random_seed', int)
auto_var.add_variable('rsep', float, default=float(-1.))
class NormVarClass(VariableClass, metaclass=RegisteringChoiceType):
    """Defines which distance measure to use for attack."""
    var_name = "norm"

    @register_var()
    @staticmethod
    def inf(auto_var):
        """L infinity norm"""
        return np.inf

    @register_var(argument='2')
    @staticmethod
    def l2(auto_var):
        """L2 norm"""
        return 2

    @register_var(argument='1')
    @staticmethod
    def l1(auto_var):
        """L1 norm"""
        return 1


auto_var.add_variable_class(NormVarClass())
auto_var.add_variable_class(DatasetVarClass())
auto_var.add_variable_class(ModelVarClass())
auto_var.add_variable_class(AttackVarClass())
auto_var.add_variable('random_seed', int)
auto_var.add_variable('eps', float)