Created
September 17, 2014 14:50
-
-
Save coela/bc332f60fcb9490932e3 to your computer and use it in GitHub Desktop.
visualizedeeplayer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from numpy.random import * | |
import pickle | |
import numpy | |
pixnum = 784 | |
pixnum2 = 100 | |
alpha = 0.1 | |
numIterations = 500 | |
def sigmoid(x): | |
return 1. / (1. + numpy.exp(-x)) | |
with open('dae1.pkl', 'rb') as f: | |
dae1 = pickle.load(f) | |
with open('dae2.pkl', 'rb') as f2: | |
dae2 = pickle.load(f2) | |
W = dae1.get_weights() | |
vmatrix = dae2.get_weights() | |
print "W: " + str(numpy.shape(vmatrix)) | |
W = W.T | |
xx = rand(pixnum,1) | |
final = numpy.zeros((pixnum,1)) | |
for col in range (0,pixnum2): | |
v = vmatrix[:,col] | |
xx = rand(pixnum,1) | |
print col | |
for i in range(0, numIterations): | |
tmp1 = sigmoid( numpy.dot(W,xx) ) * ( 1 - sigmoid(numpy.dot(W,xx))) | |
tmp2 = numpy.dot(v.T,numpy.diag(tmp1.flat)) | |
gradient = numpy.dot(tmp2,W) | |
gradient.shape = (pixnum,1) | |
xx = xx + alpha * gradient | |
if col == 0 : | |
final = xx.T | |
else : | |
final = numpy.vstack((final,xx.T)) | |
#print numpy.shape(final) | |
import gzip | |
from PIL import Image | |
def visualize_weights(weights, panel_shape, tile_size): | |
def scale(x): | |
eps = 1e-8 | |
x = x.copy() | |
x -= x.min() | |
x *= 1.0 / (x.max() + eps) | |
return 255.0*x | |
margin_y = numpy.zeros(tile_size[1]) | |
margin_x = numpy.zeros((tile_size[0] + 1) * panel_shape[0]) | |
image = margin_x.copy() | |
for y in range(panel_shape[1]): | |
foo = [ numpy.c_[ scale( x.reshape(tile_size) ), margin_y ] for x in weights[y*panel_shape[0]:(y+1)*panel_shape[0]]] | |
print foo | |
tmp = numpy.hstack(foo) | |
tmp = numpy.vstack([tmp, margin_x]) | |
image = numpy.vstack([image, tmp]) | |
img = Image.fromarray(image) | |
img = img.convert('RGB') | |
return img | |
panel_shape = (int(numpy.sqrt(final.shape[0])), int(numpy.sqrt(final.shape[0]))) | |
tile_size = (int(numpy.sqrt(final.size)), int(numpy.sqrt(final.size))) | |
img = visualize_weights(final,(10,10),(28,28)) | |
img.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment