forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize_features.py
57 lines (47 loc) · 1.74 KB
/
visualize_features.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# https://deeplearningcourses.com/c/unsupervised-deep-learning-in-python
# https://www.udemy.com/unsupervised-deep-learning-in-python
import numpy as np
import theano
import theano.tensor as T
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from theano.tensor.shared_randomstreams import RandomStreams
from util import relu, error_rate, getKaggleMNIST, init_weights
from unsupervised import DBN
from rbm import RBM
def main(loadfile=None, savefile=None):
Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST()
if loadfile:
dbn = DBN.load(loadfile)
else:
dbn = DBN([1000, 750, 500, 10]) # AutoEncoder is default
dbn = DBN([1000, 750, 500, 10], UnsupervisedModel=RBM)
dbn.fit(Xtrain, pretrain_epochs=15)
if savefile:
dbn.save(savefile)
# first layer features
# initial weight is D x M
# W = dbn.hidden_layers[0].W.eval()
# for i in xrange(dbn.hidden_layers[0].M):
# imgplot = plt.imshow(W[:,i].reshape(28, 28), cmap='gray')
# plt.show()
# should_quit = raw_input("Show more? Enter 'n' to quit\n")
# if should_quit == 'n':
# break
# features learned in the last layer
for k in xrange(dbn.hidden_layers[-1].M):
# activate the kth node
X = dbn.fit_to_input(k)
imgplot = plt.imshow(X.reshape(28, 28), cmap='gray')
plt.show()
if k < dbn.hidden_layers[-1].M - 1:
should_quit = raw_input("Show more? Enter 'n' to quit\n")
if should_quit == 'n':
break
if __name__ == '__main__':
# to load a saved file
main(loadfile='rbm15.npz')
# to neither load nor save
# main()
# to save a trained unsupervised deep network
# main(savefile='rbm15.npz')