Python lasagne.layers.ReshapeLayer() Examples
The following are 30
code examples of lasagne.layers.ReshapeLayer().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
lasagne.layers
, or try the search function
.
Example #1
Source File: models_uncond.py From EvolutionaryGAN with MIT License | 6 votes |
def build_generator_32(noise=None, ngf=128): # noise input InputNoise = InputLayer(shape=(None, 100), input_var=noise) #FC Layer gnet0 = DenseLayer(InputNoise, ngf*4*4*4, W=Normal(0.02), nonlinearity=relu) print ("Gen fc1:", gnet0.output_shape) #Reshape Layer gnet1 = ReshapeLayer(gnet0,([0],ngf*4,4,4)) print ("Gen rs1:", gnet1.output_shape) # DeConv Layer gnet2 = Deconv2DLayer(gnet1, ngf*2, (4,4), (2,2), crop=1, W=Normal(0.02),nonlinearity=relu) print ("Gen deconv1:", gnet2.output_shape) # DeConv Layer gnet3 = Deconv2DLayer(gnet2, ngf, (4,4), (2,2), crop=1, W=Normal(0.02),nonlinearity=relu) print ("Gen deconv2:", gnet3.output_shape) # DeConv Layer gnet4 = Deconv2DLayer(gnet3, 3, (4,4), (2,2), crop=1, W=Normal(0.02),nonlinearity=tanh) print ("Gen output:", gnet4.output_shape) return gnet4
Example #2
Source File: models_uncond.py From EvolutionaryGAN with MIT License | 6 votes |
def build_generator_64(noise=None, ngf=128): # noise input InputNoise = InputLayer(shape=(None, 100), input_var=noise) #FC Layer gnet0 = DenseLayer(InputNoise, ngf*8*4*4, W=Normal(0.02), nonlinearity=relu) print ("Gen fc1:", gnet0.output_shape) #Reshape Layer gnet1 = ReshapeLayer(gnet0,([0],ngf*8,4,4)) print ("Gen rs1:", gnet1.output_shape) # DeConv Layer gnet2 = Deconv2DLayer(gnet1, ngf*8, (4,4), (2,2), crop=1, W=Normal(0.02),nonlinearity=relu) print ("Gen deconv2:", gnet2.output_shape) # DeConv Layer gnet3 = Deconv2DLayer(gnet2, ngf*4, (4,4), (2,2), crop=1, W=Normal(0.02),nonlinearity=relu) print ("Gen deconv3:", gnet3.output_shape) # DeConv Layer gnet4 = Deconv2DLayer(gnet3, ngf*4, (4,4), (2,2), crop=1, W=Normal(0.02),nonlinearity=relu) print ("Gen deconv4:", gnet4.output_shape) # DeConv Layer gnet5 = Deconv2DLayer(gnet4, ngf*2, (4,4), (2,2), crop=1, W=Normal(0.02),nonlinearity=relu) print ("Gen deconv5:", gnet5.output_shape) # DeConv Layer gnet6 = Deconv2DLayer(gnet5, 3, (3,3), (1,1), crop='same', W=Normal(0.02),nonlinearity=tanh) print ("Gen output:", gnet6.output_shape) return gnet6
Example #3
Source File: wgan.py From Theano-MPI with Educational Community License v2.0 | 6 votes |
def build_critic(input_var=None): from lasagne.layers import (InputLayer, Conv2DLayer, ReshapeLayer, DenseLayer) try: from lasagne.layers.dnn import batch_norm_dnn as batch_norm except ImportError: from lasagne.layers import batch_norm from lasagne.nonlinearities import LeakyRectify lrelu = LeakyRectify(0.2) # input: (None, 1, 28, 28) layer = InputLayer(shape=(None, 1, 28, 28), input_var=input_var) # two convolutions layer = batch_norm(Conv2DLayer(layer, 64, 5, stride=2, pad='same', nonlinearity=lrelu)) layer = batch_norm(Conv2DLayer(layer, 128, 5, stride=2, pad='same', nonlinearity=lrelu)) # fully-connected layer layer = batch_norm(DenseLayer(layer, 1024, nonlinearity=lrelu)) # output layer (linear and without bias) layer = DenseLayer(layer, 1, nonlinearity=None, b=None) print ("critic output:", layer.output_shape) return layer
Example #4
Source File: lsgan.py From Theano-MPI with Educational Community License v2.0 | 6 votes |
def build_critic(input_var=None): from lasagne.layers import (InputLayer, Conv2DLayer, ReshapeLayer, DenseLayer) try: from lasagne.layers.dnn import batch_norm_dnn as batch_norm except ImportError: from lasagne.layers import batch_norm from lasagne.nonlinearities import LeakyRectify lrelu = LeakyRectify(0.2) # input: (None, 1, 28, 28) layer = InputLayer(shape=(None, 1, 28, 28), input_var=input_var) # two convolutions layer = batch_norm(Conv2DLayer(layer, 64, 5, stride=2, pad='same', nonlinearity=lrelu)) layer = batch_norm(Conv2DLayer(layer, 128, 5, stride=2, pad='same', nonlinearity=lrelu)) # fully-connected layer layer = batch_norm(DenseLayer(layer, 1024, nonlinearity=lrelu)) # output layer (linear) layer = DenseLayer(layer, 1, nonlinearity=None) print ("critic output:", layer.output_shape) return layer
Example #5
Source File: test_layers.py From ntm-lasagne with MIT License | 6 votes |
def model(input_var, batch_size=1): l_input = InputLayer((batch_size, None, 8), input_var=input_var) batch_size_var, seqlen, _ = l_input.input_var.shape # Neural Turing Machine Layer memory = Memory((128, 20), name='memory') controller = DenseController(l_input, memory_shape=(128, 20), num_units=100, num_reads=1, name='controller') heads = [ WriteHead(controller, num_shifts=3, memory_shape=(128, 20), name='write'), ReadHead(controller, num_shifts=3, memory_shape=(128, 20), name='read') ] l_ntm = NTMLayer(l_input, memory=memory, controller=controller, heads=heads) # Output Layer l_output_reshape = ReshapeLayer(l_ntm, (-1, 100)) l_output_dense = DenseLayer(l_output_reshape, num_units=8, name='dense') l_output = ReshapeLayer(l_output_dense, (batch_size_var if batch_size \ is None else batch_size, seqlen, 8)) return l_output
Example #6
Source File: models.py From recurrent-memory with GNU Lesser General Public License v3.0 | 6 votes |
def LeInitRecurrent(input_var, mask_var=None, batch_size=1, n_in=100, n_out=1, n_hid=200, diag_val=0.9, offdiag_val=0.01, out_nlin=lasagne.nonlinearities.linear): # Input Layer l_in = InputLayer((batch_size, None, n_in), input_var=input_var) if mask_var==None: l_mask=None else: l_mask = InputLayer((batch_size, None), input_var=mask_var) _, seqlen, _ = l_in.input_var.shape l_in_hid = DenseLayer(lasagne.layers.InputLayer((None, n_in)), n_hid, W=lasagne.init.GlorotNormal(0.95), nonlinearity=lasagne.nonlinearities.linear) l_hid_hid = DenseLayer(lasagne.layers.InputLayer((None, n_hid)), n_hid, W=LeInit(diag_val=diag_val, offdiag_val=offdiag_val), nonlinearity=lasagne.nonlinearities.linear) l_rec = lasagne.layers.CustomRecurrentLayer(l_in, l_in_hid, l_hid_hid, nonlinearity=lasagne.nonlinearities.rectify, mask_input=l_mask, grad_clipping=100) # Output Layer l_shp = ReshapeLayer(l_rec, (-1, n_hid)) l_dense = DenseLayer(l_shp, num_units=n_out, W=lasagne.init.GlorotNormal(0.95), nonlinearity=out_nlin) # To reshape back to our original shape, we can use the symbolic shape variables we retrieved above. l_out = ReshapeLayer(l_dense, (batch_size, seqlen, n_out)) return l_out, l_rec
Example #7
Source File: models.py From recurrent-memory with GNU Lesser General Public License v3.0 | 6 votes |
def OrthoInitRecurrent(input_var, mask_var=None, batch_size=1, n_in=100, n_out=1, n_hid=200, init_val=0.9, out_nlin=lasagne.nonlinearities.linear): # Input Layer l_in = InputLayer((batch_size, None, n_in), input_var=input_var) if mask_var==None: l_mask=None else: l_mask = InputLayer((batch_size, None), input_var=mask_var) _, seqlen, _ = l_in.input_var.shape l_in_hid = DenseLayer(lasagne.layers.InputLayer((None, n_in)), n_hid, W=lasagne.init.GlorotNormal(0.95), nonlinearity=lasagne.nonlinearities.linear) l_hid_hid = DenseLayer(lasagne.layers.InputLayer((None, n_hid)), n_hid, W=lasagne.init.Orthogonal(gain=init_val), nonlinearity=lasagne.nonlinearities.linear) l_rec = lasagne.layers.CustomRecurrentLayer(l_in, l_in_hid, l_hid_hid, nonlinearity=lasagne.nonlinearities.tanh, mask_input=l_mask, grad_clipping=100) # Output Layer l_shp = ReshapeLayer(l_rec, (-1, n_hid)) l_dense = DenseLayer(l_shp, num_units=n_out, W=lasagne.init.GlorotNormal(0.95), nonlinearity=out_nlin) # To reshape back to our original shape, we can use the symbolic shape variables we retrieved above. l_out = ReshapeLayer(l_dense, (batch_size, seqlen, n_out)) return l_out, l_rec
Example #8
Source File: models.py From recurrent-memory with GNU Lesser General Public License v3.0 | 5 votes |
def LeInitRecurrent(input_var, mask_var=None, batch_size=1, n_in=100, n_out=1, n_hid=200, diag_val=0.9, offdiag_val=0.01, out_nlin=lasagne.nonlinearities.linear): # Input Layer l_in = InputLayer((batch_size, None, n_in), input_var=input_var) if mask_var==None: l_mask=None else: l_mask = InputLayer((batch_size, None), input_var=mask_var) _, seqlen, _ = l_in.input_var.shape l_in_hid = DenseLayer(lasagne.layers.InputLayer((None, n_in)), n_hid, W=lasagne.init.GlorotNormal(0.95), nonlinearity=lasagne.nonlinearities.linear) l_hid_hid = DenseLayer(lasagne.layers.InputLayer((None, n_hid)), n_hid, W=LeInit(diag_val=diag_val, offdiag_val=offdiag_val), nonlinearity=lasagne.nonlinearities.linear) l_rec = lasagne.layers.CustomRecurrentLayer(l_in, l_in_hid, l_hid_hid, nonlinearity=lasagne.nonlinearities.rectify, mask_input=l_mask, grad_clipping=100) # Output Layer l_shp = ReshapeLayer(l_rec, (-1, n_hid)) l_dense = DenseLayer(l_shp, num_units=n_out, W=lasagne.init.GlorotNormal(0.95), nonlinearity=out_nlin) # To reshape back to our original shape, we can use the symbolic shape variables we retrieved above. l_out = ReshapeLayer(l_dense, (batch_size, seqlen, n_out)) return l_out, l_rec
Example #9
Source File: lsgan_cifar10.py From Theano-MPI with Educational Community License v2.0 | 5 votes |
def build_generator(input_var=None, verbose=False): from lasagne.layers import InputLayer, ReshapeLayer, DenseLayer try: from lasagne.layers import TransposedConv2DLayer as Deconv2DLayer except ImportError: raise ImportError("Your Lasagne is too old. Try the bleeding-edge " "version: http://lasagne.readthedocs.io/en/latest/" "user/installation.html#bleeding-edge-version") try: from lasagne.layers.dnn import batch_norm_dnn as batch_norm except ImportError: from lasagne.layers import batch_norm from lasagne.nonlinearities import sigmoid # input: 100dim layer = InputLayer(shape=(None, 100), input_var=input_var) # # fully-connected layer # layer = batch_norm(DenseLayer(layer, 1024)) # project and reshape layer = batch_norm(DenseLayer(layer, 1024*4*4)) layer = ReshapeLayer(layer, ([0], 1024, 4, 4)) # two fractional-stride convolutions layer = batch_norm(Deconv2DLayer(layer, 512, 5, stride=2, crop='same', output_size=8)) layer = batch_norm(Deconv2DLayer(layer, 256, 5, stride=2, crop='same', output_size=16)) layer = Deconv2DLayer(layer, 3, 5, stride=2, crop='same', output_size=32, nonlinearity=sigmoid) if verbose: print ("Generator output:", layer.output_shape) return layer
Example #10
Source File: models.py From recurrent-memory with GNU Lesser General Public License v3.0 | 5 votes |
def LeInitRecurrentWithFastWeights(input_var, mask_var=None, batch_size=1, n_in=100, n_out=1, n_hid=200, diag_val=0.9, offdiag_val=0.01, out_nlin=lasagne.nonlinearities.linear, gamma=0.9): # Input Layer l_in = InputLayer((batch_size, None, n_in), input_var=input_var) if mask_var==None: l_mask=None else: l_mask = InputLayer((batch_size, None), input_var=mask_var) _, seqlen, _ = l_in.input_var.shape l_in_hid = DenseLayer(lasagne.layers.InputLayer((None, n_in)), n_hid, W=lasagne.init.GlorotNormal(0.95), nonlinearity=lasagne.nonlinearities.linear) l_hid_hid = DenseLayer(lasagne.layers.InputLayer((None, n_hid)), n_hid, W=LeInit(diag_val=diag_val, offdiag_val=offdiag_val), nonlinearity=lasagne.nonlinearities.linear) l_rec = CustomRecurrentLayerWithFastWeights(l_in, l_in_hid, l_hid_hid, nonlinearity=lasagne.nonlinearities.rectify, mask_input=l_mask, grad_clipping=100, gamma=gamma) # Output Layer l_shp = ReshapeLayer(l_rec, (-1, n_hid)) l_dense = DenseLayer(l_shp, num_units=n_out, W=lasagne.init.GlorotNormal(0.95), nonlinearity=out_nlin) # To reshape back to our original shape, we can use the symbolic shape variables we retrieved above. l_out = ReshapeLayer(l_dense, (batch_size, seqlen, n_out)) return l_out, l_rec
Example #11
Source File: models.py From recurrent-memory with GNU Lesser General Public License v3.0 | 5 votes |
def GRURecurrent(input_var, mask_var=None, batch_size=1, n_in=100, n_out=1, n_hid=200, diag_val=0.9, offdiag_val=0.01, out_nlin=lasagne.nonlinearities.linear): # Input Layer l_in = InputLayer((batch_size, None, n_in), input_var=input_var) if mask_var==None: l_mask = None else: l_mask = InputLayer((batch_size, None), input_var=mask_var) _, seqlen, _ = l_in.input_var.shape l_rec = GRULayer(l_in, n_hid, resetgate=lasagne.layers.Gate(W_in=lasagne.init.GlorotNormal(0.05), W_hid=lasagne.init.GlorotNormal(0.05), W_cell=None, b=lasagne.init.Constant(0.)), updategate=lasagne.layers.Gate(W_in=lasagne.init.GlorotNormal(0.05), W_hid=lasagne.init.GlorotNormal(0.05), W_cell=None), hidden_update=lasagne.layers.Gate(W_in=lasagne.init.GlorotNormal(0.05), W_hid=LeInit(diag_val=diag_val, offdiag_val=offdiag_val), W_cell=None, nonlinearity=lasagne.nonlinearities.rectify), hid_init = lasagne.init.Constant(0.), backwards=False, learn_init=False, gradient_steps=-1, grad_clipping=10., unroll_scan=False, precompute_input=True, mask_input=l_mask, only_return_final=False) # Output Layer l_shp = ReshapeLayer(l_rec, (-1, n_hid)) l_dense = DenseLayer(l_shp, num_units=n_out, W=lasagne.init.GlorotNormal(0.05), nonlinearity=out_nlin) # To reshape back to our original shape, we can use the symbolic shape variables we retrieved above. l_out = ReshapeLayer(l_dense, (batch_size, seqlen, n_out)) return l_out, l_rec
Example #12
Source File: sort-task.py From ntm-lasagne with MIT License | 5 votes |
def model(input_var, batch_size=1, size=8, num_units=100, memory_shape=(128, 20)): # Input Layer l_input = InputLayer((batch_size, None, size + 1), input_var=input_var) _, seqlen, _ = l_input.input_var.shape # Neural Turing Machine Layer memory = Memory(memory_shape, name='memory', memory_init=lasagne.init.Constant(1e-6), learn_init=False) controller = GRUController(l_input, memory_shape=memory_shape, num_units=num_units, num_reads=1, nonlinearity=lasagne.nonlinearities.rectify, name='controller') heads = [ WriteHead(controller, num_shifts=3, memory_shape=memory_shape, name='write', learn_init=False, nonlinearity_key=lasagne.nonlinearities.rectify, nonlinearity_add=lasagne.nonlinearities.rectify), ReadHead(controller, num_shifts=3, memory_shape=memory_shape, name='read', learn_init=False, nonlinearity_key=lasagne.nonlinearities.rectify) ] l_ntm = NTMLayer(l_input, memory=memory, controller=controller, heads=heads) # Output Layer l_output_reshape = ReshapeLayer(l_ntm, (-1, num_units)) l_output_dense = DenseLayer(l_output_reshape, num_units=size + 1, nonlinearity=lasagne.nonlinearities.sigmoid, \ name='dense') l_output = ReshapeLayer(l_output_dense, (batch_size, seqlen, size + 1)) return l_output, l_ntm
Example #13
Source File: reversed-copy-task.py From ntm-lasagne with MIT License | 5 votes |
def model(input_var, batch_size=1, size=8, num_units=100, memory_shape=(128, 20)): # Input Layer l_input = InputLayer((batch_size, None, size + 1), input_var=input_var) _, seqlen, _ = l_input.input_var.shape # Neural Turing Machine Layer memory = Memory(memory_shape, name='memory', memory_init=lasagne.init.Constant(1e-6), learn_init=False) controller = RecurrentController(l_input, memory_shape=memory_shape, num_units=num_units, num_reads=1, nonlinearity=lasagne.nonlinearities.rectify, name='controller') heads = [ WriteHead(controller, num_shifts=3, memory_shape=memory_shape, name='write', learn_init=False, nonlinearity_key=lasagne.nonlinearities.rectify, nonlinearity_add=lasagne.nonlinearities.rectify), ReadHead(controller, num_shifts=3, memory_shape=memory_shape, name='read', learn_init=False, nonlinearity_key=lasagne.nonlinearities.rectify) ] l_ntm = NTMLayer(l_input, memory=memory, controller=controller, heads=heads) # Output Layer l_output_reshape = ReshapeLayer(l_ntm, (-1, num_units)) l_output_dense = DenseLayer(l_output_reshape, num_units=size + 1, nonlinearity=lasagne.nonlinearities.sigmoid, \ name='recurrent') l_output = ReshapeLayer(l_output_dense, (batch_size, seqlen, size + 1)) return l_output, l_ntm
Example #14
Source File: upsidedown-copy-task.py From ntm-lasagne with MIT License | 5 votes |
def model(input_var, batch_size=1, size=8, num_units=100, memory_shape=(128, 20)): # Input Layer l_input = InputLayer((batch_size, None, size + 1), input_var=input_var) _, seqlen, _ = l_input.input_var.shape # Neural Turing Machine Layer memory = Memory(memory_shape, name='memory', memory_init=lasagne.init.Constant(1e-6), learn_init=False) controller = DenseController(l_input, memory_shape=memory_shape, num_units=num_units, num_reads=1, nonlinearity=lasagne.nonlinearities.rectify, name='controller') heads = [ WriteHead(controller, num_shifts=3, memory_shape=memory_shape, name='write', learn_init=False, nonlinearity_key=lasagne.nonlinearities.rectify, nonlinearity_add=lasagne.nonlinearities.rectify), ReadHead(controller, num_shifts=3, memory_shape=memory_shape, name='read', learn_init=False, nonlinearity_key=lasagne.nonlinearities.rectify) ] l_ntm = NTMLayer(l_input, memory=memory, controller=controller, heads=heads) # Output Layer l_output_reshape = ReshapeLayer(l_ntm, (-1, num_units)) l_output_dense = DenseLayer(l_output_reshape, num_units=size + 1, nonlinearity=lasagne.nonlinearities.sigmoid, \ name='dense') l_output = ReshapeLayer(l_output_dense, (batch_size, seqlen, size + 1)) return l_output, l_ntm
Example #15
Source File: associative-recall-task.py From ntm-lasagne with MIT License | 5 votes |
def model(input_var, batch_size=1, size=8, num_units=100, memory_shape=(128, 20)): # Input Layer l_input = InputLayer((batch_size, None, size + 2), input_var=input_var) _, seqlen, _ = l_input.input_var.shape # Neural Turing Machine Layer memory = Memory(memory_shape, name='memory', memory_init=lasagne.init.Constant(1e-6), learn_init=False) controller = DenseController(l_input, memory_shape=memory_shape, num_units=num_units, num_reads=1, nonlinearity=lasagne.nonlinearities.rectify, name='controller') heads = [ WriteHead(controller, num_shifts=3, memory_shape=memory_shape, name='write', learn_init=False, nonlinearity_key=lasagne.nonlinearities.rectify, nonlinearity_add=lasagne.nonlinearities.rectify), ReadHead(controller, num_shifts=3, memory_shape=memory_shape, name='read', learn_init=False, nonlinearity_key=lasagne.nonlinearities.rectify) ] l_ntm = NTMLayer(l_input, memory=memory, controller=controller, heads=heads) # Output Layer l_output_reshape = ReshapeLayer(l_ntm, (-1, num_units)) l_output_dense = DenseLayer(l_output_reshape, num_units=size + 2, nonlinearity=lasagne.nonlinearities.sigmoid, \ name='dense') l_output = ReshapeLayer(l_output_dense, (batch_size, seqlen, size + 2)) return l_output, l_ntm
Example #16
Source File: copy-task.py From ntm-lasagne with MIT License | 5 votes |
def model(input_var, batch_size=1, size=8, num_units=100, memory_shape=(128, 20)): # Input Layer l_input = InputLayer((batch_size, None, size + 1), input_var=input_var) _, seqlen, _ = l_input.input_var.shape # Neural Turing Machine Layer memory = Memory(memory_shape, name='memory', memory_init=lasagne.init.Constant(1e-6), learn_init=False) controller = DenseController(l_input, memory_shape=memory_shape, num_units=num_units, num_reads=1, nonlinearity=lasagne.nonlinearities.rectify, name='controller') heads = [ WriteHead(controller, num_shifts=3, memory_shape=memory_shape, name='write', learn_init=False, nonlinearity_key=lasagne.nonlinearities.rectify, nonlinearity_add=lasagne.nonlinearities.rectify), ReadHead(controller, num_shifts=3, memory_shape=memory_shape, name='read', learn_init=False, nonlinearity_key=lasagne.nonlinearities.rectify) ] l_ntm = NTMLayer(l_input, memory=memory, controller=controller, heads=heads) # Output Layer l_output_reshape = ReshapeLayer(l_ntm, (-1, num_units)) l_output_dense = DenseLayer(l_output_reshape, num_units=size + 1, nonlinearity=lasagne.nonlinearities.sigmoid, \ name='dense') l_output = ReshapeLayer(l_output_dense, (batch_size, seqlen, size + 1)) return l_output, l_ntm
Example #17
Source File: dyck-words-task.py From ntm-lasagne with MIT License | 5 votes |
def model(input_var, batch_size=1, num_units=100, memory_shape=(128, 20)): # Input Layer l_input = InputLayer((batch_size, None, 1), input_var=input_var) _, seqlen, _ = l_input.input_var.shape # Neural Turing Machine Layer memory = Memory(memory_shape, name='memory', memory_init=lasagne.init.Constant(1e-6), learn_init=False) controller = DenseController(l_input, memory_shape=memory_shape, num_units=num_units, num_reads=1, nonlinearity=lasagne.nonlinearities.rectify, name='controller') heads = [ WriteHead(controller, num_shifts=3, memory_shape=memory_shape, name='write', learn_init=False, nonlinearity_key=lasagne.nonlinearities.rectify, nonlinearity_add=lasagne.nonlinearities.rectify), ReadHead(controller, num_shifts=3, memory_shape=memory_shape, name='read', learn_init=False, nonlinearity_key=lasagne.nonlinearities.rectify) ] l_ntm = NTMLayer(l_input, memory=memory, controller=controller, heads=heads) # Output Layer l_output_reshape = ReshapeLayer(l_ntm, (-1, num_units)) l_output_dense = DenseLayer(l_output_reshape, num_units=1, nonlinearity=lasagne.nonlinearities.sigmoid, \ name='dense') l_output = ReshapeLayer(l_output_dense, (batch_size, seqlen, 1)) return l_output, l_ntm
Example #18
Source File: lsgan.py From Theano-MPI with Educational Community License v2.0 | 5 votes |
def build_generator(input_var=None): from lasagne.layers import InputLayer, ReshapeLayer, DenseLayer try: from lasagne.layers import TransposedConv2DLayer as Deconv2DLayer except ImportError: raise ImportError("Your Lasagne is too old. Try the bleeding-edge " "version: http://lasagne.readthedocs.io/en/latest/" "user/installation.html#bleeding-edge-version") try: from lasagne.layers.dnn import batch_norm_dnn as batch_norm except ImportError: from lasagne.layers import batch_norm from lasagne.nonlinearities import sigmoid # input: 100dim layer = InputLayer(shape=(None, 100), input_var=input_var) # fully-connected layer layer = batch_norm(DenseLayer(layer, 1024)) # project and reshape layer = batch_norm(DenseLayer(layer, 128*7*7)) layer = ReshapeLayer(layer, ([0], 128, 7, 7)) # two fractional-stride convolutions layer = batch_norm(Deconv2DLayer(layer, 64, 5, stride=2, crop='same', output_size=14)) layer = Deconv2DLayer(layer, 1, 5, stride=2, crop='same', output_size=28, nonlinearity=sigmoid) print ("Generator output:", layer.output_shape) return layer
Example #19
Source File: wgan.py From Theano-MPI with Educational Community License v2.0 | 5 votes |
def build_generator(input_var=None): from lasagne.layers import InputLayer, ReshapeLayer, DenseLayer try: from lasagne.layers import TransposedConv2DLayer as Deconv2DLayer except ImportError: raise ImportError("Your Lasagne is too old. Try the bleeding-edge " "version: http://lasagne.readthedocs.io/en/latest/" "user/installation.html#bleeding-edge-version") try: from lasagne.layers.dnn import batch_norm_dnn as batch_norm except ImportError: from lasagne.layers import batch_norm from lasagne.nonlinearities import sigmoid # input: 100dim layer = InputLayer(shape=(None, 100), input_var=input_var) # fully-connected layer layer = batch_norm(DenseLayer(layer, 1024)) # project and reshape layer = batch_norm(DenseLayer(layer, 128*7*7)) layer = ReshapeLayer(layer, ([0], 128, 7, 7)) # two fractional-stride convolutions layer = batch_norm(Deconv2DLayer(layer, 64, 5, stride=2, crop='same', output_size=14)) layer = Deconv2DLayer(layer, 1, 5, stride=2, crop='same', output_size=28, nonlinearity=sigmoid) print ("Generator output:", layer.output_shape) return layer
Example #20
Source File: models.py From recurrent-memory with GNU Lesser General Public License v3.0 | 5 votes |
def GRURecurrent(input_var, mask_var=None, batch_size=1, n_in=100, n_out=1, n_hid=200, diag_val=0.9, offdiag_val=0.01, out_nlin=lasagne.nonlinearities.linear): # Input Layer l_in = InputLayer((batch_size, None, n_in), input_var=input_var) if mask_var==None: l_mask = None else: l_mask = InputLayer((batch_size, None), input_var=mask_var) _, seqlen, _ = l_in.input_var.shape l_rec = GRULayer(l_in, n_hid, resetgate=lasagne.layers.Gate(W_in=lasagne.init.GlorotNormal(0.05), W_hid=lasagne.init.GlorotNormal(0.05), W_cell=None, b=lasagne.init.Constant(0.)), updategate=lasagne.layers.Gate(W_in=lasagne.init.GlorotNormal(0.05), W_hid=lasagne.init.GlorotNormal(0.05), W_cell=None), hidden_update=lasagne.layers.Gate(W_in=lasagne.init.GlorotNormal(0.05), W_hid=LeInit(diag_val=diag_val, offdiag_val=offdiag_val), W_cell=None, nonlinearity=lasagne.nonlinearities.rectify), hid_init = lasagne.init.Constant(0.), backwards=False, learn_init=False, gradient_steps=-1, grad_clipping=10., unroll_scan=False, precompute_input=True, mask_input=l_mask, only_return_final=False) # Output Layer l_shp = ReshapeLayer(l_rec, (-1, n_hid)) l_dense = DenseLayer(l_shp, num_units=n_out, W=lasagne.init.GlorotNormal(0.05), nonlinearity=out_nlin) # To reshape back to our original shape, we can use the symbolic shape variables we retrieved above. l_out = ReshapeLayer(l_dense, (batch_size, seqlen, n_out)) return l_out, l_rec
Example #21
Source File: models.py From recurrent-memory with GNU Lesser General Public License v3.0 | 5 votes |
def LeInitRecurrentWithFastWeights(input_var, mask_var=None, batch_size=1, n_in=100, n_out=1, n_hid=200, diag_val=0.9, offdiag_val=0.01, out_nlin=lasagne.nonlinearities.linear, gamma=0.9): # Input Layer l_in = InputLayer((batch_size, None, n_in), input_var=input_var) if mask_var==None: l_mask=None else: l_mask = InputLayer((batch_size, None), input_var=mask_var) _, seqlen, _ = l_in.input_var.shape l_in_hid = DenseLayer(lasagne.layers.InputLayer((None, n_in)), n_hid, W=lasagne.init.GlorotNormal(0.95), nonlinearity=lasagne.nonlinearities.linear) l_hid_hid = DenseLayer(lasagne.layers.InputLayer((None, n_hid)), n_hid, W=LeInit(diag_val=diag_val, offdiag_val=offdiag_val), nonlinearity=lasagne.nonlinearities.linear) l_rec = CustomRecurrentLayerWithFastWeights(l_in, l_in_hid, l_hid_hid, nonlinearity=lasagne.nonlinearities.rectify, mask_input=l_mask, grad_clipping=100, gamma=gamma) # Output Layer l_shp = ReshapeLayer(l_rec, (-1, n_hid)) l_dense = DenseLayer(l_shp, num_units=n_out, W=lasagne.init.GlorotNormal(0.95), nonlinearity=out_nlin) # To reshape back to our original shape, we can use the symbolic shape variables we retrieved above. l_out = ReshapeLayer(l_dense, (batch_size, seqlen, n_out)) return l_out, l_rec
Example #22
Source File: models.py From recurrent-memory with GNU Lesser General Public License v3.0 | 5 votes |
def TanhRecurrent(input_var, mask_var=None, batch_size=1, n_in=100, n_out=1, n_hid=200, wscale=1.0, out_nlin=lasagne.nonlinearities.linear): # Input Layer l_in = InputLayer((batch_size, None, n_in), input_var=input_var) if mask_var == None: l_mask = None else: l_mask = InputLayer((batch_size, None), input_var=mask_var) _, seqlen, _ = l_in.input_var.shape l_in_hid = DenseLayer(lasagne.layers.InputLayer((None, n_in)), n_hid, W=lasagne.init.HeNormal(0.95), nonlinearity=lasagne.nonlinearities.linear) l_hid_hid = DenseLayer(lasagne.layers.InputLayer((None, n_hid)), n_hid, W=lasagne.init.HeNormal(gain=wscale), nonlinearity=lasagne.nonlinearities.linear) l_rec = lasagne.layers.CustomRecurrentLayer(l_in, l_in_hid, l_hid_hid, nonlinearity=lasagne.nonlinearities.tanh, mask_input=l_mask, grad_clipping=100) l_shp_1 = ReshapeLayer(l_rec, (-1, n_hid)) l_shp_2 = ReshapeLayer(l_hid_hid, (-1, n_hid)) l_shp = lasagne.layers.ElemwiseSumLayer((l_shp_1,l_shp_2),coeffs=(np.float32(0.2),np.float32(0.8))) # Output Layer l_dense = DenseLayer(l_shp, num_units=n_out, W=lasagne.init.HeNormal(0.95), nonlinearity=out_nlin) # To reshape back to our original shape, we can use the symbolic shape variables we retrieved above. l_out = ReshapeLayer(l_dense, (batch_size, seqlen, n_out)) return l_out, l_rec
Example #23
Source File: eeg_cnn_lib.py From EEGLearn with GNU General Public License v2.0 | 5 votes |
def build_convpool_lstm(input_vars, nb_classes, grad_clip=110, imsize=32, n_colors=3, n_timewin=7): """ Builds the complete network with LSTM layer to integrate time from sequences of EEG images. :param input_vars: list of EEG images (one image per time window) :param nb_classes: number of classes :param grad_clip: the gradient messages are clipped to the given value during the backward pass. :param imsize: size of the input image (assumes a square input) :param n_colors: number of color channels in the image :param n_timewin: number of time windows in the snippet :return: a pointer to the output of last layer """ convnets = [] w_init = None # Build 7 parallel CNNs with shared weights for i in range(n_timewin): if i == 0: convnet, w_init = build_cnn(input_vars[i], imsize=imsize, n_colors=n_colors) else: convnet, _ = build_cnn(input_vars[i], w_init=w_init, imsize=imsize, n_colors=n_colors) convnets.append(FlattenLayer(convnet)) # at this point convnets shape is [numTimeWin][n_samples, features] # we want the shape to be [n_samples, features, numTimeWin] convpool = ConcatLayer(convnets) convpool = ReshapeLayer(convpool, ([0], n_timewin, get_output_shape(convnets[0])[1])) # Input to LSTM should have the shape as (batch size, SEQ_LENGTH, num_features) convpool = LSTMLayer(convpool, num_units=128, grad_clipping=grad_clip, nonlinearity=lasagne.nonlinearities.tanh) # We only need the final prediction, we isolate that quantity and feed it # to the next layer. convpool = SliceLayer(convpool, -1, 1) # Selecting the last prediction # A fully-connected layer of 256 units with 50% dropout on its inputs: convpool = DenseLayer(lasagne.layers.dropout(convpool, p=.5), num_units=256, nonlinearity=lasagne.nonlinearities.rectify) # And, finally, the output layer with 50% dropout on its inputs: convpool = DenseLayer(lasagne.layers.dropout(convpool, p=.5), num_units=nb_classes, nonlinearity=lasagne.nonlinearities.softmax) return convpool
Example #24
Source File: eeg_cnn_lib.py From EEGLearn with GNU General Public License v2.0 | 5 votes |
def build_convpool_conv1d(input_vars, nb_classes, imsize=32, n_colors=3, n_timewin=7): """ Builds the complete network with 1D-conv layer to integrate time from sequences of EEG images. :param input_vars: list of EEG images (one image per time window) :param nb_classes: number of classes :param imsize: size of the input image (assumes a square input) :param n_colors: number of color channels in the image :param n_timewin: number of time windows in the snippet :return: a pointer to the output of last layer """ convnets = [] w_init = None # Build 7 parallel CNNs with shared weights for i in range(n_timewin): if i == 0: convnet, w_init = build_cnn(input_vars[i], imsize=imsize, n_colors=n_colors) else: convnet, _ = build_cnn(input_vars[i], w_init=w_init, imsize=imsize, n_colors=n_colors) convnets.append(FlattenLayer(convnet)) # at this point convnets shape is [numTimeWin][n_samples, features] # we want the shape to be [n_samples, features, numTimeWin] convpool = ConcatLayer(convnets) convpool = ReshapeLayer(convpool, ([0], n_timewin, get_output_shape(convnets[0])[1])) convpool = DimshuffleLayer(convpool, (0, 2, 1)) # input to 1D convlayer should be in (batch_size, num_input_channels, input_length) convpool = Conv1DLayer(convpool, 64, 3) # A fully-connected layer of 512 units with 50% dropout on its inputs: convpool = DenseLayer(lasagne.layers.dropout(convpool, p=.5), num_units=512, nonlinearity=lasagne.nonlinearities.rectify) # And, finally, the output layer with 50% dropout on its inputs: convpool = DenseLayer(lasagne.layers.dropout(convpool, p=.5), num_units=nb_classes, nonlinearity=lasagne.nonlinearities.softmax) return convpool
Example #25
Source File: models_uncond.py From EvolutionaryGAN with MIT License | 5 votes |
def build_generator_128(noise=None, ngf=128): lrelu = LeakyRectify(0.2) # noise input InputNoise = InputLayer(shape=(None, 100), input_var=noise) #FC Layer gnet0 = DenseLayer(InputNoise, ngf*16*4*4, W=Normal(0.02), nonlinearity=lrelu) print ("Gen fc1:", gnet0.output_shape) #Reshape Layer gnet1 = ReshapeLayer(gnet0,([0],ngf*16,4,4)) print ("Gen rs1:", gnet1.output_shape) # DeConv Layer gnet2 = Deconv2DLayer(gnet1, ngf*8, (4,4), (2,2), crop=1, W=Normal(0.02),nonlinearity=lrelu) print ("Gen deconv1:", gnet2.output_shape) # DeConv Layer gnet3 = Deconv2DLayer(gnet2, ngf*8, (4,4), (2,2), crop=1, W=Normal(0.02),nonlinearity=lrelu) print ("Gen deconv2:", gnet3.output_shape) # DeConv Layer gnet4 = Deconv2DLayer(gnet3, ngf*4, (4,4), (2,2), crop=1, W=Normal(0.02),nonlinearity=lrelu) print ("Gen deconv3:", gnet4.output_shape) # DeConv Layer gnet5 = Deconv2DLayer(gnet4, ngf*4, (4,4), (2,2), crop=1, W=Normal(0.02),nonlinearity=lrelu) print ("Gen deconv4:", gnet5.output_shape) # DeConv Layer gnet6 = Deconv2DLayer(gnet5, ngf*2, (4,4), (2,2), crop=1, W=Normal(0.02),nonlinearity=lrelu) print ("Gen deconv5:", gnet6.output_shape) # DeConv Layer gnet7 = Deconv2DLayer(gnet6, 3, (3,3), (1,1), crop='same', W=Normal(0.02),nonlinearity=tanh) print ("Gen output:", gnet7.output_shape) return gnet7
Example #26
Source File: mlp.py From drmad with MIT License | 4 votes |
def __init__(self, rng, rstream, x, y, setting): # add cost """ Constructing the mlp model. Arguments: rng, rstream - random streams """ self.paramsEle = [] self.paramsHyper = [] self.layers = [ll.InputLayer((None, 3, 28, 28))] self.layers.append(ll.ReshapeLayer(self.layers[-1], (None, 3*28*28))) penalty = 0. for num in [1000, 1000, 1000, 10]: # TODO: refactor it later self.layers.append(DenseLayerWithReg(setting, self.layers[-1], num_units=num)) self.paramsEle += self.layers[-1].W self.paramsEle += self.layers[-1].b if setting.regL2 is not None: tempL2 = self.layers[-1].L2 * T.sqr(self.layers[-1].W) penalty += T.sum(tempL2) self.paramsHyper += self.layers[-1].L2 self.y = self.layers[-1].output self.prediction = T.argmax(self.y, axis=1) self.penalty = penalty if penalty != 0. else T.constant(0.) def stable(x, stabilize=True): if stabilize: x = T.where(T.isnan(x), 1000., x) x = T.where(T.isinf(x), 1000., x) return x if setting.cost == 'categorical_crossentropy': def costFun1(y, label): return stable(-T.log(y[T.arange(label.shape[0]), label]), stabilize=True) else: raise NotImplementedError def costFunT1(*args, **kwargs): return T.mean(costFun1(*args, **kwargs)) # cost function self.trainCost = costFunT1(self.y, y) self.classError = T.mean(T.cast(T.neq(self.guessLabel, y), 'float32'))
Example #27
Source File: agent_rl.py From KB-InfoBot with MIT License | 4 votes |
def _init_model(self, in_size, out_size, n_hid=10, learning_rate_sl=0.005, \ learning_rate_rl=0.005, batch_size=32, ment=0.1): # 2-layer MLP self.in_size = in_size # x and y coordinate self.out_size = out_size # up, down, right, left self.batch_size = batch_size self.learning_rate = learning_rate_rl self.n_hid = n_hid input_var, turn_mask, act_mask, reward_var = T.ftensor3('in'), T.imatrix('tm'), \ T.itensor3('am'), T.fvector('r') in_var = T.reshape(input_var, (input_var.shape[0]*input_var.shape[1],self.in_size)) l_mask_in = L.InputLayer(shape=(None,None), input_var=turn_mask) pol_in = T.fmatrix('pol-h') l_in = L.InputLayer(shape=(None,None,self.in_size), input_var=input_var) l_pol_rnn = L.GRULayer(l_in, n_hid, hid_init=pol_in, mask_input=l_mask_in) # B x H x D pol_out = L.get_output(l_pol_rnn)[:,-1,:] l_den_in = L.ReshapeLayer(l_pol_rnn, (turn_mask.shape[0]*turn_mask.shape[1], n_hid)) # BH x D l_out = L.DenseLayer(l_den_in, self.out_size, nonlinearity=lasagne.nonlinearities.softmax) self.network = l_out self.params = L.get_all_params(self.network) # rl probs = L.get_output(self.network) # BH x A out_probs = T.reshape(probs, (input_var.shape[0],input_var.shape[1],self.out_size)) # B x H x A log_probs = T.log(out_probs) act_probs = (log_probs*act_mask).sum(axis=2) # B x H ep_probs = (act_probs*turn_mask).sum(axis=1) # B H_probs = -T.sum(T.sum(out_probs*log_probs,axis=2),axis=1) # B self.loss = 0.-T.mean(ep_probs*reward_var + ment*H_probs) updates = lasagne.updates.rmsprop(self.loss, self.params, learning_rate=learning_rate_rl, \ epsilon=1e-4) self.inps = [input_var, turn_mask, act_mask, reward_var, pol_in] self.train_fn = theano.function(self.inps, self.loss, updates=updates) self.obj_fn = theano.function(self.inps, self.loss) self.act_fn = theano.function([input_var, turn_mask, pol_in], [out_probs, pol_out]) # sl sl_loss = 0.-T.mean(ep_probs) sl_updates = lasagne.updates.rmsprop(sl_loss, self.params, learning_rate=learning_rate_sl, \ epsilon=1e-4) self.sl_train_fn = theano.function([input_var, turn_mask, act_mask, pol_in], sl_loss, \ updates=sl_updates) self.sl_obj_fn = theano.function([input_var, turn_mask, act_mask, pol_in], sl_loss)
Example #28
Source File: e576.py From neuralnilm with Apache License 2.0 | 4 votes |
def ae(batch): NUM_FILTERS = 8 input_shape = batch.input.shape target_shape = batch.target.shape seq_length = input_shape[1] input_layer = InputLayer( shape=input_shape ) # Dense layers dense_layer_0 = DenseLayer( input_layer, num_units=(seq_length - 3) * NUM_FILTERS ) dense_layer_1 = DenseLayer( dense_layer_0, num_units=128 ) dense_layer_2 = DenseLayer( dense_layer_1, num_units=(seq_length - 3) * NUM_FILTERS ) # Output final_dense_layer = DenseLayer( dense_layer_2, num_units=target_shape[1] * target_shape[2], nonlinearity=None ) output_layer = ReshapeLayer( final_dense_layer, shape=target_shape ) net = Net( output_layer, tags=['AE'], description="Like AE in e575 but much larger layers. Still no conv layers.", predecessor_experiment="e575" ) return net # ------------------------ DATA ----------------------
Example #29
Source File: e575.py From neuralnilm with Apache License 2.0 | 4 votes |
def ae(batch): input_shape = batch.input.shape target_shape = batch.target.shape input_layer = InputLayer( shape=input_shape ) # Dense layers dense_layer_0 = DenseLayer( input_layer, num_units=128 ) dense_layer_1 = DenseLayer( dense_layer_0, num_units=64 ) dense_layer_2 = DenseLayer( dense_layer_1, num_units=128 ) # Output final_dense_layer = DenseLayer( dense_layer_2, num_units=target_shape[1] * target_shape[2], nonlinearity=None ) output_layer = ReshapeLayer( final_dense_layer, shape=target_shape ) net = Net( output_layer, tags=['AE'], description="Like AE in e567 but without conv layer", predecessor_experiment="e567" ) return net # ------------------------ DATA ----------------------
Example #30
Source File: eeg_cnn_lib.py From EEGLearn with GNU General Public License v2.0 | 4 votes |
def build_convpool_mix(input_vars, nb_classes, grad_clip=110, imsize=32, n_colors=3, n_timewin=7): """ Builds the complete network with LSTM and 1D-conv layers combined :param input_vars: list of EEG images (one image per time window) :param nb_classes: number of classes :param grad_clip: the gradient messages are clipped to the given value during the backward pass. :param imsize: size of the input image (assumes a square input) :param n_colors: number of color channels in the image :param n_timewin: number of time windows in the snippet :return: a pointer to the output of last layer """ convnets = [] w_init = None # Build 7 parallel CNNs with shared weights for i in range(n_timewin): if i == 0: convnet, w_init = build_cnn(input_vars[i], imsize=imsize, n_colors=n_colors) else: convnet, _ = build_cnn(input_vars[i], w_init=w_init, imsize=imsize, n_colors=n_colors) convnets.append(FlattenLayer(convnet)) # at this point convnets shape is [numTimeWin][n_samples, features] # we want the shape to be [n_samples, features, numTimeWin] convpool = ConcatLayer(convnets) convpool = ReshapeLayer(convpool, ([0], n_timewin, get_output_shape(convnets[0])[1])) reformConvpool = DimshuffleLayer(convpool, (0, 2, 1)) # input to 1D convlayer should be in (batch_size, num_input_channels, input_length) conv_out = Conv1DLayer(reformConvpool, 64, 3) conv_out = FlattenLayer(conv_out) # Input to LSTM should have the shape as (batch size, SEQ_LENGTH, num_features) lstm = LSTMLayer(convpool, num_units=128, grad_clipping=grad_clip, nonlinearity=lasagne.nonlinearities.tanh) lstm_out = SliceLayer(lstm, -1, 1) # Merge 1D-Conv and LSTM outputs dense_input = ConcatLayer([conv_out, lstm_out]) # A fully-connected layer of 256 units with 50% dropout on its inputs: convpool = DenseLayer(lasagne.layers.dropout(dense_input, p=.5), num_units=512, nonlinearity=lasagne.nonlinearities.rectify) # And, finally, the 10-unit output layer with 50% dropout on its inputs: convpool = DenseLayer(convpool, num_units=nb_classes, nonlinearity=lasagne.nonlinearities.softmax) return convpool