def read_gmm(stream, *_): v, covariance_type = stream.read_singleton("covariance_type") v, dimension = stream.read_scalar("dimension", int) v, num_components = stream.read_scalar("num_components", int) v, relevances = stream.read_list("relevances", float) v, gmm_weights = stream.read_array("weights", rtype=float, dim=1, shape=(num_components,)) v, smms = stream.read_indexed_collection(read_smm, (covariance_type, num_components, dimension), name="Gaussians") gmm_means = numpy.zeros((num_components, dimension), dtype=float) if covariance_type is GaussianMixtureModel.FULL_COVARIANCE: var_shape = (num_components, dimension, dimension) else: assert covariance_type is GaussianMixtureModel.DIAGONAL_COVARIANCE var_shape = (num_components, dimension) gmm_vars = numpy.zeros(var_shape, dtype=float) assert len(smms) == num_components for i in xrange(num_components): gmm_means[i] = smms[i].means gmm_vars[i] = smms[i].vars # Construct and return Gmm object ret = GaussianMixtureModel(dimension, covariance_type, num_components) ret.set_weights(gmm_weights) ret.set_means(gmm_means) ret.set_vars(gmm_vars) ret.set_relevances(relevances) return ret
def read_gmm(stream, *_): v, covariance_type = stream.read_singleton("covariance_type") v, dimension = stream.read_scalar("dimension", int) v, num_components = stream.read_scalar("num_components", int) v, relevances = stream.read_list("relevances", float) v, gmm_weights = stream.read_array("weights", rtype=float, dim=1, shape=(num_components, )) v, smms = stream.read_indexed_collection( read_smm, (covariance_type, num_components, dimension), name="Gaussians") gmm_means = numpy.zeros((num_components, dimension), dtype=float) if covariance_type is GaussianMixtureModel.FULL_COVARIANCE: var_shape = (num_components, dimension, dimension) else: assert (covariance_type is GaussianMixtureModel.DIAGONAL_COVARIANCE) var_shape = (num_components, dimension) gmm_vars = numpy.zeros(var_shape, dtype=float) assert (len(smms) == num_components) for i in xrange(num_components): gmm_means[i] = smms[i].means gmm_vars[i] = smms[i].vars # Construct and return Gmm object ret = GaussianMixtureModel(dimension, covariance_type, num_components) ret.set_weights(gmm_weights) ret.set_means(gmm_means) ret.set_vars(gmm_vars) ret.set_relevances(relevances) return ret
def make_target(dimension, num_comps, weights, means, vars): ret = GaussianMixtureModel(dimension, GaussianModelBase.DIAGONAL_COVARIANCE, num_comps) ret.set_weights(numpy.array(weights)) ret.set_means(numpy.array(means)) ret.set_vars(numpy.array(vars)) return ret