def mixtureMV(self, mu, sig, ps):
"""LearningRules.mixtureMV
Sample from a multivariate gaussian mixture with \mu = means, \Sigma = cov matrix
"""
# print "%s.mixtureMV: Implement me" % (self.__class__.__name__)
# print "mixtureMV", "mu", mu.shape, "sig", sig.shape, "ps", ps.shape
mixcomps = self.mixcomps
d = self.dim
assert mixcomps == ps.shape[0]
assert not np.isnan(mu).any()
assert not np.isnan(sig).any()
assert not np.isnan(ps).any()
# check flat inputs
if mu.shape[1] == 1:
mu = mu.reshape((self.mixcomps, self.dim))
# mu_ = mu.reshape((mixcomps, d))
if sig.shape[1] == 1:
# S_diag = sig[:(mixcomps * self.dim)]
# S_triu = sig[(mixcomps * self.dim):]
S_diag = sig[:(mixcomps * d)].reshape((mixcomps, d))
S_triu = sig[(mixcomps * d):].reshape((mixcomps, -1))
S = []
for i in range(mixcomps):
S_ = np.diag(S_diag[i])
S_[np.triu_indices(d, 1)] = S_triu[i]
S_[np.tril_indices(d, -1)] = S_triu[i] # check if thats correct
S.append(S_)
sig = np.array(S)
# print "mixtureMV", "mu", mu.shape, "sig", sig.shape, "ps", ps.shape
# d = mu.shape[0]
compidx = self.mixture_sample_prior(ps)
mu_ = mu[compidx]
S_ = sig[compidx]
# print "compidx", compidx, "mu_", mu_, "S_", S_
y = np.random.multivariate_normal(mu_, S_)
return y
# mixture, mdn_loss, and softmax are taken from karpathy's MixtureDensityNets.py
评论列表
文章目录