Last active
March 7, 2019 12:13
-
-
Save DFoly/c32e9936832875655cc71d0c20d0f364 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def _m_step(self, X, gamma): | |
| """Performs M-step of the GMM | |
| We need to update our priors, our means | |
| and our covariance matrix. | |
| Parameters: | |
| ----------- | |
| X: (N x d), data | |
| gamma: (N x C), posterior distribution of lower bound | |
| Returns: | |
| --------- | |
| pi: (C) | |
| mu: (C x d) | |
| sigma: (C x d x d) | |
| """ | |
| N = X.shape[0] # number of objects | |
| C = self.gamma.shape[1] # number of clusters | |
| d = X.shape[1] # dimension of each object | |
| # responsibilities for each gaussian | |
| self.pi = np.mean(self.gamma, axis = 0) | |
| self.mu = np.dot(self.gamma.T, X) / np.sum(self.gamma, axis = 0)[:,np.newaxis] | |
| for c in range(C): | |
| x = X - self.mu[c, :] # (N x d) | |
| gamma_diag = np.diag(self.gamma[:,c]) | |
| x_mu = np.matrix(x) | |
| gamma_diag = np.matrix(gamma_diag) | |
| sigma_c = x.T * gamma_diag * x | |
| self.sigma[c,:,:]=(sigma_c) / np.sum(self.gamma, axis = 0)[:,np.newaxis][c] | |
| return self.pi, self.mu, self.sigma | |
| def _compute_loss_function(self, X, pi, mu, sigma): | |
| """Computes lower bound loss function | |
| Parameters: | |
| ----------- | |
| X: (N x d), data | |
| Returns: | |
| --------- | |
| pi: (C) | |
| mu: (C x d) | |
| sigma: (C x d x d) | |
| """ | |
| N = X.shape[0] | |
| C = self.gamma.shape[1] | |
| self.loss = np.zeros((N, C)) | |
| for c in range(C): | |
| dist = mvn(self.mu[c], self.sigma[c],allow_singular=True) | |
| self.loss[:,c] = self.gamma[:,c] * (np.log(self.pi[c]+0.00001)+dist.logpdf(X)-np.log(self.gamma[:,c]+0.000001)) | |
| self.loss = np.sum(self.loss) | |
| return self.loss |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment