(loadfile=None, savefile=None)
| 18 | |
| 19 | |
| 20 | def main(loadfile=None, savefile=None): |
| 21 | Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() |
| 22 | if loadfile: |
| 23 | dbn = DBN.load(loadfile) |
| 24 | else: |
| 25 | dbn = DBN([1000, 750, 500, 10]) # AutoEncoder is default |
| 26 | # dbn = DBN([1000, 750, 500, 10], UnsupervisedModel=RBM) |
| 27 | dbn.fit(Xtrain, pretrain_epochs=2) |
| 28 | |
| 29 | if savefile: |
| 30 | dbn.save(savefile) |
| 31 | |
| 32 | # first layer features |
| 33 | # initial weight is D x M |
| 34 | W = dbn.hidden_layers[0].W.eval() |
| 35 | for i in range(dbn.hidden_layers[0].M): |
| 36 | imgplot = plt.imshow(W[:,i].reshape(28, 28), cmap='gray') |
| 37 | plt.show() |
| 38 | should_quit = input("Show more? Enter 'n' to quit\n") |
| 39 | if should_quit == 'n': |
| 40 | break |
| 41 | |
| 42 | # features learned in the last layer |
| 43 | for k in range(dbn.hidden_layers[-1].M): |
| 44 | # activate the kth node |
| 45 | X = dbn.fit_to_input(k) |
| 46 | imgplot = plt.imshow(X.reshape(28, 28), cmap='gray') |
| 47 | plt.show() |
| 48 | if k < dbn.hidden_layers[-1].M - 1: |
| 49 | should_quit = input("Show more? Enter 'n' to quit\n") |
| 50 | if should_quit == 'n': |
| 51 | break |
| 52 | |
| 53 | |
| 54 | if __name__ == '__main__': |
no test coverage detected