comparison autoencoder-specgram.py @ 1:04f1e3463466 tip master

Implement maxpooling and unpooling aspect
author Dan Stowell <danstowell@users.sourceforge.net>
date Wed, 13 Jan 2016 09:56:16 +0000
parents 73317239d6d1
children
comparison
equal deleted inserted replaced
0:73317239d6d1 1:04f1e3463466
12 import theano.tensor as T 12 import theano.tensor as T
13 import lasagne 13 import lasagne
14 #import downhill 14 #import downhill
15 from lasagne.nonlinearities import rectify, leaky_rectify, very_leaky_rectify 15 from lasagne.nonlinearities import rectify, leaky_rectify, very_leaky_rectify
16 from numpy import float32 16 from numpy import float32
17
18 try:
19 from lasagne.layers import InverseLayer as _
20 use_maxpool = True
21 except ImportError:
22 print("""**********************
23 WARNING: InverseLayer not found in Lasagne. Please use a more recent version of Lasagne.
24 WARNING: We'll deactivate the maxpooling part of the network (since we can't use InverseLayer to undo it)""")
25 use_maxpool = False
17 26
18 import matplotlib 27 import matplotlib
19 #matplotlib.use('PDF') # http://www.astrobetter.com/plotting-to-a-file-in-python/ 28 #matplotlib.use('PDF') # http://www.astrobetter.com/plotting-to-a-file-in-python/
20 import matplotlib.pyplot as plt 29 import matplotlib.pyplot as plt
21 import matplotlib.cm as cm 30 import matplotlib.cm as cm
63 network = NormalisationLayer(network, specbinnum) 72 network = NormalisationLayer(network, specbinnum)
64 normlayer = network # we need to remember this one so we can set its parameters 73 normlayer = network # we need to remember this one so we can set its parameters
65 # 74 #
66 network, filters_enc = make_custom_convlayer(network, in_num_chans=specbinnum, out_num_chans=numfilters) 75 network, filters_enc = make_custom_convlayer(network, in_num_chans=specbinnum, out_num_chans=numfilters)
67 # 76 #
68 ########################################### 77 # NOTE: here we're using max-pooling, along the time axis only, and then
69 ########################################### 78 # using Lasagne's "InverseLayer" to undo the maxpooling in one-hot fashion.
70 ########################################### 79 # There's a side-effect of this: if you use *overlapping* maxpooling windows,
71 # NOTE: in this example we have no maxpooling or other downsampling. 80 # the InverseLayer may behave slightly unexpectedly, adding some points with
72 # That's a notable absence compared to standard architectures. 81 # double magnitude. It's OK here since we're not overlapping the windows
73 # For the present code, with 1 encoding layer and 1 decoding layer, 82 if use_maxpool:
74 # you might expect that to happen about here. 83 network = lasagne.layers.MaxPool2DLayer(network, pool_size=(1,2), stride=(1,2))
75 ########################################### 84 maxpool_layer = network # store a pointer to this one
76 ########################################### 85
77 ########################################### 86 # NOTE: HERE is the "middle" of the autoencoder!
78
79 latents = network # we remember the "latents" at the midpoint of the net, since we'll want to inspect them, and maybe regularise them too 87 latents = network # we remember the "latents" at the midpoint of the net, since we'll want to inspect them, and maybe regularise them too
88
89 if use_maxpool:
90 network = lasagne.layers.InverseLayer(network, maxpool_layer)
80 91
81 network, filters_dec = make_custom_convlayer(network, in_num_chans=numfilters, out_num_chans=specbinnum) 92 network, filters_dec = make_custom_convlayer(network, in_num_chans=numfilters, out_num_chans=specbinnum)
82 93
83 network = lasagne.layers.NonlinearityLayer(network, nonlinearity=rectify) # finally a standard rectify since nonneg (specgram) target 94 network = lasagne.layers.NonlinearityLayer(network, nonlinearity=rectify) # finally a standard rectify since nonneg (specgram) target
84 95