import numpy as np from kmeans import KMeans from numpy.linalg import LinAlgError from…
- User
import numpy as np from kmeans import KMeans from numpy.linalg import LinAlgError from tqdm import tqdm
SIGMA_CONST = 1e-06 LOG_CONST = 1e-32 FULL_MATRIX = False
class GMM(object):
def init(self, X, K, max_iters=100): """ Args: X: the observations/datapoints, N x D numpy array K: number of clusters/components max_iters: maximum number of iterations (used in EM implementation) """ self.points = X self.max_iters = max_iters self.N = self.points.shape[0] self.D = self.points.shape[1] self.K = K
def softmax(self, logit): """ Args: logit: N x D numpy array Return: prob: N x D numpy array. See the above function. Hint: Add keepdims=True in your np.sum() function to avoid broadcast error. """ logit_sub = logit - logit.max(axis=1, keepdims=True) exp_logit = np.exp(logit_sub) sum_exp_logit = np.sum(exp_logit, axis=1, keepdims=True) return exp_logit / sum_exp_logit
def logsumexp(self, logit): """ Args: logit: N x D numpy array Return: s: N x 1 array where s[i,0] = logsumexp(logit[i,:]). See the above function Hint: The keepdims parameter could be handy """ logit_max = np.max(logit, axis=1, keepdims=True) return logit_max + np.log( np.sum(np.exp(logit - logit_max), axis=1, keepdims=True) )
def normalPDF(self, points, mu_i, sigma_i): """ Args: points: N x D numpy array mu_i: (D,) numpy array, the center for the ith gaussian. sigma_i: DxD numpy array, the covariance matrix of the ith gaussian. Return: pdf: (N,) numpy array, the probability density value of N data for the ith gaussian
Hint: np.diagonal() should be handy. """ variances = np.diagonal(sigma_i) variances = np.maximum(variances, SIGMA_CONST) log_coeff = -0.5 * np.log(2 * np.pi * variances) log_exp_part = -0.5 * ((points - mu_i) ** 2) / variances log_pdf = np.sum(log_coeff + log_exp_part, axis=1) pdf = np.exp(log_pdf) return pdf
def multinormalPDF(self, points, mu_i, sigma_i): """ Args: points: N x D numpy array mu_i: (D,) numpy array, the center for the ith gaussian. sigma_i: DxD numpy array, the covariance matrix of the ith gaussian. Return: normal_pdf: (N,) numpy array, the probability density value of N data for the ith gaussian
Hint:
- np.linalg.det() and np.linalg.inv() should be handy.
- Note the value in self.D may be outdated and not correspond to the current dataset.
- You may wanna check if the matrix is singular before implementing calculation process. """ raise NotImplementedError
def create_pi(self): """ Initialize the prior probabilities Args: Return: pi: numpy array of length K, prior """ return np.ones(self.K) / self.K
def create_mu(self): """ Intialize random centers for each gaussian Args: Return: mu: KxD numpy array, the center for each gaussian. """ random_indices = np.random.choice(self.N, self.K, replace=False) mu = self.points[random_indices, :] return mu
def create_sigma(self): """ Initialize the covariance matrix with np.eye() for each k. For grads, you can also initialize the by K diagonal matrices. Args: Return: sigma: KxDxD numpy array, the diagonal standard deviation of each gaussian. You will have KxDxD numpy array for full covariance matrix case """ return np.repeat(np.eye(self.D)[np.newaxis, :, :], self.K, axis=0)
- Assistant