def load_snapshot(self, model, epoch: int): path = self.get_snapshot_path(epoch) if not os.path.exists(path): return None logger.debug('loading snapshot epoch %d from %s', epoch, path) model.load_weights(path)
def dump_snapshot(self, model, epoch: int): if not os.path.exists( os.path.join(self.exp_absolute_path, self.snapshots_dir_relative_path)): os.makedirs( os.path.join(self.exp_absolute_path, self.snapshots_dir_relative_path)) path = self.get_snapshot_path(epoch) logger.debug('saving snapshot epoch %d at %s', epoch, path) model.save_weights(path)
def load_config(path, handler=None): import os if not os.path.exists(path): raise RuntimeError('could not find path: ' + path) if os.path.isdir(path): new_path = os.path.join(path, 'config.json') logger.debug('found a directory at path:', path, '\nLooking for a config file in:', new_path) path = new_path handler = get_handler(handler or 'DefaultLocal', instantiate=False) return handler.load_config(path)
def kfold_cross_validation(self, n_splits): """ Evaluates the precision of the classifier by running its prediction on the training data set :param n_splits: number of splits to split the training data into :type n_splits: int :return: the percentage of correctly predicted categories :rtype: float """ if n_splits == 1: return np.mean( self.predict(self._clf_data.data) == self._clf_data.target) length = len(self._clf_data.data) sub_length = int(length / n_splits) logger.info( "Training data splitted in {} splits, each of length of {}, for a total length of {}" .format(n_splits, sub_length, length)) splits = [] for i in range(0, n_splits): split = ClassifierData( self._clf_data.data[i:i + sub_length], self._clf_data.target[i:i + sub_length], self._clf_data.target_names[i:i + sub_length]) splits.append(split) split_length = len(split.data) total = 0 precisions = [] for split in splits: training_data = ClassifierData([], [], self.target_names) total += len(split.data) for other_split in splits: if other_split != split: for i in range(len(other_split.data)): training_data.data.append(other_split.data[i]) for i in range(len(other_split.data)): training_data.target.append(other_split.target[i]) #type(self) will invoke the right constructor for children test_classifier = type(self)(training_data.data, training_data.target, training_data.target_names) test_classifier.train() logger.debug("Trained on split {}".format(split)) precision = np.mean( test_classifier.predict(split.data) == split.target) logger.debug("Precision on other split {} : {}".format( split, precision)) precisions.append(precision) logger.debug("Total added splits length: {}".format(total)) return np.mean(precisions)
def get_feature(feature_name, **params): assert isinstance(feature_name, str) logger.debug('Fetching %s Feature', feature_name) return mapping[feature_name.lower()](**params)