def get_states(ds, instances, action_inds, mi=None): state = tc.TimelyState(ds.action_dims) cost = ds.action_costs[action_inds].sum() / ds.max_budget assert(cost <= 1) N = instances.shape[0] states = np.zeros((N, state.S)) for i in xrange(N): states[i] = state.get_state(instances[i], action_inds, cost) # Impute unobserved values. if mi is not None: states = mi.impute(states) return states
def get_states_from_mask_distribution(ds, md, instances, labels, N, mi=None): state = tc.TimelyState(ds.action_dims) mask = md.sample(N) ind = np.random.choice(instances.shape[0], N) instances = instances[ind] costs = np.dot(mask, ds.action_costs) states = state.get_states_from_mask(instances, mask, costs) # Impute unobserved values. if mi is not None: states = mi.impute(states) labels = labels[ind] return states, labels
def __init__(self, action_dims): self.state = tc.TimelyState(action_dims) self.has_been_fit = False
def __init__(self, ds): self.ds = ds self.F = len(self.ds.actions) self.state = tc.TimelyState(self.ds.action_dims)
def __init__(self, ds, num_clf): self.ds = ds self.num_clf = num_clf self.num_classes = len(ds.labels) self.state = tc.TimelyState(self.ds.action_dims) self.has_been_fit = False
def __init__(self, data_source, log_dirname=None, max_iter=5, min_iter=2, batch_size=.25, max_batches=5, random_start=False, policy_feat='dynamic', policy_method='linear', rewards_mode='auc', rewards_loss='infogain', gamma=1, epsilons_mode='exp', normalize_reward_locally=False, num_clf=1, clf_method='logreg', add_fully_observed=False, loss='zero_one', impute_method='0', **args): def check_null(x): return x is None or isinstance(x, types.FloatType) and np.isnan(x) FINAL_ITER_EPSILON = 0.01 data_source.validate() self.ds = data_source assert (max_iter > 0) self.max_iter = max_iter self.min_iter = min_iter assert (batch_size > 0. and batch_size <= 1.) self.batch_size = batch_size self.max_batches = max_batches self.random_start = random_start self.policy_feat = None self.policy_method = policy_method if policy_method == 'all': self.policy = None elif policy_method == 'random': self.policy = tc.policy.RandomPolicy(self.ds) elif policy_method == 'manual_orthants': if not isinstance(self.ds, tc.data_sources.SyntheticOrthants): raise Exception( 'ManualOrthants policy only defined for SyntheticOrthants') self.policy = tc.policy.ManualOrthantsPolicy(self.ds) else: self.policy_feat = policy_feat combination = (policy_feat, policy_method) if combination == ('static', 'linear'): self.policy = tc.policy.StaticLinearPolicy(self.ds) elif combination == ('static', 'linear_untaken'): self.policy = tc.policy.StaticLinearUntakenPolicy(self.ds) elif combination == ('dynamic', 'linear'): self.policy = tc.policy.LinearPolicy(self.ds) elif combination == ('dynamic', 'linear_untaken'): self.policy = tc.policy.LinearUntakenPolicy(self.ds) else: raise Exception( "This (policy_feat, policy_method) combo not supported!") if check_null(epsilons_mode): self.epsilons_mode = None self.epsilons = None else: if epsilons_mode == 'exp': norm = self.max_iter / np.log(FINAL_ITER_EPSILON) self.epsilons = np.exp(np.arange(self.max_iter) / norm) elif epsilons_mode == 'zero': self.epsilons = np.zeros(self.max_iter) else: raise Exception("This epsilons mode is not implemented!") self.epsilons = np.round(self.epsilons, 3).tolist() self.epsilons_mode = epsilons_mode if clf_method == 'logreg_old': self.classifier = tc.classifier.LogisticClassifier( self.ds, num_clf) elif clf_method == 'logreg': self.classifier = tc.StateClassifier(self.ds.action_dims, len(self.ds.labels), num_clf, max_masks=100) elif clf_method == 'imagenet': self.classifier = tc.StateClassifierImagenet(self.ds) elif clf_method == 'sgd': self.classifier = tc.classifier.SGDClassifier(self.ds, num_clf) elif clf_method == 'gnb': self.classifier = tc.classifier.GaussianNBClassifier( self.ds, num_clf) else: raise Exception( "This clf_method ({}) is not supported!".format(clf_method)) self.num_clf = num_clf self.clf_method = clf_method self.add_fully_observed = add_fully_observed if loss == 'zero_one': self.loss = tc.evaluation.zero_one_loss else: raise Exception("This loss ({}) is not implemented!".format(loss)) self.info_loss = tc.evaluation.info_loss assert (impute_method in ['0', 'mean', 'gaussian']) self.impute_method = impute_method if self.impute_method == 'mean': self.imputer = tc.MeanImputer(self.ds.action_dims) elif self.impute_method == 'gaussian': self.imputer = tc.GaussianImputer(self.ds.action_dims) else: self.imputer = None if check_null(rewards_mode): self.rewards_mode = 'auc' else: assert (rewards_mode in ['auc', 'final']) self.rewards_mode = rewards_mode if check_null(rewards_loss): self.rewards_loss_ = self.info_loss self.rewards_loss = None else: if rewards_loss == 'loss': self.rewards_loss_ = self.loss elif rewards_loss == 'infogain': self.rewards_loss_ = self.info_loss else: raise Exception("This rewards mode is not implemented!") self.rewards_loss = rewards_loss if check_null(gamma): self.gamma = None else: assert (gamma >= 0 and gamma <= 1) self.gamma = gamma self.normalize_reward_locally = normalize_reward_locally self.state = tc.TimelyState(self.ds.action_dims) if log_dirname is None: self.logging_dirname = tempfile.gettempdir() report_filename = '/dev/null' else: log_dirname = os.path.relpath(log_dirname, tc.repo_dir) self.logging_dirname = '{}/{}/{}'.format(log_dirname, self.ds.name, self.name) tc.util.mkdir_p(self.logging_dirname) report_filename = self.logging_dirname + '.html' self.report = tc.Report(self.logging_dirname, report_filename) self.report.info = self.__config__() self.has_been_fit = False
def __init__(self, action_dims, num_labels, num_clf=1, max_masks=None): self.num_labels = num_labels self.state = tc.TimelyState(action_dims) self.num_clf = num_clf self.max_masks = max_masks self.has_been_fit = False
def __init__(self, ds): # Need datasource because need feature name and bound information ds.validate() self.ds = ds self.state = tc.TimelyState(ds.action_dims)