def __init__(self, primary_classifier, variant_labels): self._variant_labels = variant_labels self._variant_models = {} for label in variant_labels: model = SimpleGaussianModel() # KJB - this guy shouldn't have to know so much about models - # maybe do some kind of cloning from models in the classifier? model.set_model(primary_classifier.num_features, 'diagonal') self._variant_models[label] = model self._primary = primary_classifier self._variant_classifier = None
def make_model_samplers(means, vars): """ >>> SimpleGaussianModel.seed(0) >>> tuple(int(128 * sampler()) for sampler in text_utils.make_model_samplers((0, 1), (1, 0.5))) (-23, 130) >>> tuple(int(128 * sampler()) for sampler in text_utils.make_model_samplers((0, 1, 2), (1, 0.5, 2))) (89, 119, 511) """ assert len(means) == len(vars) num_classes = len(means) models = tuple( SimpleGaussianModel(1, SimpleGaussianModel.DIAGONAL_COVARIANCE) for i in xrange(num_classes)) for model, mean, var in izip(models, means, vars): model.set_model((mean, ), (var, )) samplers = tuple(model.sample for model in models) assert len(samplers) == num_classes return samplers
def read_smm(stream, params, _): covariance_type, num_components, dimension = params v, smm_means = stream.read_array("means", rtype=float, dim=1, shape=(dimension, )) if covariance_type is GaussianMixtureModel.FULL_COVARIANCE: var_dim = 2 var_shape = (dimension, dimension) else: assert (covariance_type is GaussianMixtureModel.DIAGONAL_COVARIANCE) var_dim = 1 var_shape = (dimension, ) v, smm_vars = stream.read_array("vars", rtype=float, dim=var_dim, shape=var_shape) ret = SimpleGaussianModel(dimension, covariance_type) ret.set_model(smm_means, smm_vars) return ret
def make_data_generator(dimension): # Data generator setup and data generation target_means = ((1, 1), (2, 2), (3, 3)) target_vars = ((0.1, 0.1), (0.2, 0.2), (0.3, 0.3)) target_durations = (2, 6, 6) assert len(target_means) == len(target_vars) == len(target_durations) num_generators = len(target_means) generators = [ SimpleGaussianModel(dimension, SimpleGaussianModel.DIAGONAL_COVARIANCE) for i in xrange(num_generators) ] for (m, tm, tv) in izip(generators, target_means, target_vars): m.set_model(tm, tv) SimpleGaussianModel.seed(0) def obs_generator(): while True: yield [ m.sample() for m, d in izip(generators, target_durations) for i in xrange(d) ] return obs_generator()
def __init__(self, labels, dimension): super(SimpleClassifier, self).__init__(labels, dimension) for label in labels: m = SimpleGaussianModel(dimension, SimpleGaussianModel.DIAGONAL_COVARIANCE) self._models[label] = m
def __init__(self, labels, nfeatures): self.labels = list(labels) self.nfeatures = nfeatures self.models = tuple(SimpleGaussianModel(nfeatures, GaussianModelBase.DIAGONAL_COVARIANCE) for _ in self.labels) self.samples_seen = [0] * len(self.models)
labels = True, False ncomps = 7, 3 nfeatures = numceps # mfcc, with c0: gathered from about 60,000 frames of 'train' data from Ken and Hugh skyping true_means_prime = N.array((-5.5522, -1.0517, 1.0240, 3.1609, 0.5628, -122.5789), dtype=N.float32) true_vars_prime = N.array((26.2238, 26.4064, 59.7242, 35.1180, 47.2471, 3967.2240), dtype=N.float32) false_means_prime = N.array((-9.4634, -5.3991, -4.2773, 1.7494, -0.0822, -228.6211), dtype=N.float32) false_vars_prime = N.array((3.0097, 6.0277, 8.3711, 10.7198, 13.4285, 456.7074), dtype=N.float32) # mfcc, no c0: 20,000 frames of Hugh talking true_means_prime = N.array((-4.8087, 3.9863, -0.5217, 1.3076, 0.7514, -4.6497), dtype=N.float32) true_vars_prime = N.array((26.8496, 32.6631, 32.3662, 24.2963, 36.2244, 34.1555), dtype=N.float32) false_means_prime = N.array((-6.8806, -1.3424, -3.8147, 0.4520, 0.7129, -3.1560), dtype=N.float32) false_vars_prime = N.array((2.7468, 6.2286, 7.4355, 10.1530, 13.3865, 15.9309), dtype=N.float32) true_prime = SimpleGaussianModel(nfeatures, GaussianModelBase.DIAGONAL_COVARIANCE) true_prime.set_model(true_means_prime, true_vars_prime) false_prime = SimpleGaussianModel(nfeatures, GaussianModelBase.DIAGONAL_COVARIANCE) false_prime.set_model(false_means_prime, false_vars_prime) primer = (true_prime, false_prime) GaussianMixtureModel.seed(0) gmm_mgr0 = GmmMgr(ncomps, nfeatures, GaussianModelBase.DIAGONAL_COVARIANCE, primer) gmm_mgr1 = GmmMgr(ncomps, nfeatures, GaussianModelBase.DIAGONAL_COVARIANCE, primer) classify0 = AdaptingGmmClassifier(gmm_mgr0, izip(labels, count())) classify1 = AdaptingGmmClassifier(gmm_mgr1, izip(labels, count())) classify0.set_relevance(333) classify1.set_relevance(333) classify0.set_num_em_iterations(2) classify1.set_num_em_iterations(2)