• Sign Up
• Login
• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Nov 13th, 2018 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def compute_mean_mles(train_data, train_labels):
2.     '''
3.    Compute the mean estimate for each digit class
4.
5.    Should return a numpy array of size (10,64)
6.    The ith row will correspond to the mean estimate for digit class i
7.    '''
8.     means = np.zeros((10, 64))
9.     # Compute means
10.     return means
11.
12. def compute_sigma_mles(train_data, train_labels):
13.     '''
14.    Compute the covariance estimate for each digit class
15.
16.    Should return a three dimensional numpy array of shape (10, 64, 64)
17.    consisting of a covariance matrix for each digit class
18.    '''
19.     covariances = np.zeros((10, 64, 64))
20.     mus = compute_mean_mles(train_data,train_labels)
21.     identity_add = np.identity(64)*0.01
22.     for i in range(10):
23.         c = train_data[train_labels == i,:] - mus[i]
24.         covariances[i,:,:] = (np.dot(c.T,c) / (train_labels == i).sum()) + identity_add
25.     return covariances
26.
27. def generative_likelihood(digits, means, covariances):
28.     '''
29.    Compute the generative log-likelihood:
30.        log p(x|y,mu,Sigma)
31.
32.    Should return an n x 10 numpy array
33.    '''
34.     likelihoods = np.zeros((digits.shape,10))
35.     for d in range(digits.shape):
36.         for i in range(10):
37.             x = digits[d,:]
38.             mu = means[i,:]
39.             cov = covariances[i]
40.             d = 64
41.             det_cv = np.log(np.linalg.det(cov))
42.             last_term = d*np.log(2*np.pi)
43.             likelihoods[d,i] = 0.5*(-det_cv - np.linalg.multi_dot([(x-mu).T,np.linalg.inv(cov),(x-mu)]) - last_term)
44.     return likelihoods
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top