Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def compute_mean_mles(train_data, train_labels):
- '''
- Compute the mean estimate for each digit class
- Should return a numpy array of size (10,64)
- The ith row will correspond to the mean estimate for digit class i
- '''
- means = np.zeros((10, 64))
- # Compute means
- return means
- def compute_sigma_mles(train_data, train_labels):
- '''
- Compute the covariance estimate for each digit class
- Should return a three dimensional numpy array of shape (10, 64, 64)
- consisting of a covariance matrix for each digit class
- '''
- covariances = np.zeros((10, 64, 64))
- mus = compute_mean_mles(train_data,train_labels)
- identity_add = np.identity(64)*0.01
- for i in range(10):
- c = train_data[train_labels == i,:] - mus[i]
- covariances[i,:,:] = (np.dot(c.T,c) / (train_labels == i).sum()) + identity_add
- return covariances
- def generative_likelihood(digits, means, covariances):
- '''
- Compute the generative log-likelihood:
- log p(x|y,mu,Sigma)
- Should return an n x 10 numpy array
- '''
- likelihoods = np.zeros((digits.shape,10))
- for d in range(digits.shape[0]):
- for i in range(10):
- x = digits[d,:]
- mu = means[i,:]
- cov = covariances[i]
- d = 64
- det_cv = np.log(np.linalg.det(cov))
- last_term = d*np.log(2*np.pi)
- likelihoods[d,i] = 0.5*(-det_cv - np.linalg.multi_dot([(x-mu).T,np.linalg.inv(cov),(x-mu)]) - last_term)
- return likelihoods
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement