Commit c9d3f966 authored by Emmanuel PIGNAT's avatar Emmanuel PIGNAT
Browse files

changing name

parent 3f67efec
......@@ -163,7 +163,7 @@ class GMM(Model):
mvn._lmbda = ss.block_diag([self.lmbda[i] for i in q])
return mvn
def compute_resp(self, demo=None, dep=None, table=None, marginal=None, ):
def compute_resp(self, demo=None, dep=None, table=None, marginal=None, norm=True):
sample_size = demo.shape[0]
B = np.ones((self.nb_states, sample_size))
......@@ -186,15 +186,22 @@ class GMM(Model):
B[[i], :] *= multi_variate_normal(demo, mu[i, d],
sigma[dGrid][:, :, 0], log=False)
B *= self.priors[:, None]
return B/np.sum(B, axis=0)
if norm:
return B/np.sum(B, axis=0)
else:
return B
def init_params_scikit(self, data):
def init_params_scikit(self, data, cov_type='full'):
from sklearn.mixture import BayesianGaussianMixture, GaussianMixture
gmm_init = GaussianMixture(self.nb_states, 'full', n_init=10, init_params='random')
gmm_init = GaussianMixture(self.nb_states, cov_type, n_init=5, init_params='random')
gmm_init.fit(data)
self.mu = gmm_init.means_
self.sigma = gmm_init.covariances_
if cov_type == 'diag':
self.sigma = np.array([np.diag(gmm_init.covariances_[i]) for i in range(self.nb_states)])
else:
self.sigma = gmm_init.covariances_
self.priors = gmm_init.weights_
self.Trans = np.ones((self.nb_states, self.nb_states)) * 0.01
......@@ -227,7 +234,8 @@ class GMM(Model):
self.priors = np.ones(self.nb_states) / self.nb_states
def em(self, data, reg=1e-8, maxiter=100, minstepsize=1e-5, diag=False, reg_finish=False,
kmeans_init=False, random_init=True, dep_mask=None, verbose=False, only_scikit=False):
kmeans_init=False, random_init=True, dep_mask=None, verbose=False, only_scikit=False,
no_init=False):
"""
:param data: [np.array([nb_timesteps, nb_dim])]
......@@ -256,14 +264,16 @@ class GMM(Model):
nb_samples = data.shape[0]
if random_init:
self.init_params_random(data)
elif kmeans_init:
self.init_params_kmeans(data)
else:
self.init_params_scikit(data)
if not no_init:
if random_init and not only_scikit:
self.init_params_random(data)
elif kmeans_init and not only_scikit:
self.init_params_kmeans(data)
else:
if diag:
self.init_params_scikit(data, 'diag')
else:
self.init_params_scikit(data, 'full')
if only_scikit: return
data = data.T
......
......@@ -140,7 +140,22 @@ class MVN(object):
self.sigma = np.cov(data.T)
self.lmbda = np.linalg.inv(self.sigma)
def log_prob(self, x):
def log_prob(self, x, marginal=None, reg=None):
"""
:param x:
:param marginal:
:type marginal: slice
:return:
"""
if marginal is not None:
_mu = self.mu[marginal]
_sigma = self.sigma[marginal, marginal]
if reg is not None:
_sigma += np.eye(marginal.stop-marginal.start) * reg
return multi_variate_normal(x, _mu, _sigma)
return multi_variate_normal(x, self.mu, self.sigma)
def transform(self, A, b=None, dA=None, db=None):
......
......@@ -395,7 +395,8 @@ def plot_gmm(Mu, Sigma, dim=None, color=[1, 0, 0], alpha=0.5, linewidth=1, marke
Note- Daniel Berio, switched matrix layout to be consistent with pbdlib matlab,
probably breaks with gmm now.
'''
Mu = np.array(Mu)
Sigma = np.array(Sigma)
if (Mu.ndim == 1):
if not swap:
Mu = Mu[:, np.newaxis]
......@@ -522,6 +523,7 @@ def plot_gmm(Mu, Sigma, dim=None, color=[1, 0, 0], alpha=0.5, linewidth=1, marke
def plot_gaussian(mu, sigma, dim=None, color='r', alpha=0.5, lw=1, markersize=6,
ax=None, plots=None, nb_segm=24, **kwargs):
mu, sigma = np.array(mu), np.array(sigma)
t = np.linspace(-np.pi, np.pi, nb_segm)
R = np.real(sp.linalg.sqrtm(1.0 * sigma))
......@@ -542,7 +544,8 @@ def plot_gaussian(mu, sigma, dim=None, color='r', alpha=0.5, lw=1, markersize=6,
return center, line
def plot_y_gaussian(x, mu, sigma, dim=0, alpha=1., alpha_fill=None, color='r', lw=1.):
def plot_y_gaussian(x, mu, sigma, dim=0, alpha=1., alpha_fill=None, color='r', lw=1.,
ax=None):
"""
:param mu: [n_states]
......@@ -557,8 +560,11 @@ def plot_y_gaussian(x, mu, sigma, dim=0, alpha=1., alpha_fill=None, color='r', l
if alpha_fill is None:
alpha_fill = 0.4 * alpha
plt.plot(x, mu[:, dim], alpha=alpha, color=color)
plt.fill_between(x,
if ax is None:
ax = plt
ax.plot(x, mu[:, dim], alpha=alpha, color=color)
ax.fill_between(x,
mu[:, dim] - sigma[:, dim, dim] ** 0.5,
mu[:, dim] + sigma[:, dim, dim] ** 0.5,
alpha=alpha_fill, color=color)
......
......@@ -41,5 +41,5 @@ setup(name='pbdlib',
license='MIT',
packages=find_packages(),
data_files = dlist,
install_requires = {'numpy','scipy','matplotlib','sklearn', 'dtw', 'jupyter', 'enum', 'termcolor'},
install_requires = ['numpy','scipy','matplotlib','sklearn', 'dtw', 'jupyter', 'enum', 'termcolor'],
zip_safe=False)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment