1

I am trying to build a CNN using Lasagne and I have 119 feature variables. I am able to successfully build a MLP using Lasagne but when I try to add a Convolutional layer (using 1D convolution) using the following code, I get an error. Am I doing something wrong? Has the 1D convolution in Lasagne been tested?

def build_custom_mlp(input_var, depth, width):
    network = lasagne.layers.InputLayer(shape=(None, 119), input_var=input_var)
    network = lasagne.layers.Conv1DLayer(network, num_filters=20, filter_size=10,
              stride=1, pad=1, nonlinearity=lasagne.nonlinearities.rectify)
    network = lasagne.layers.Pool1DLayer(network, pool_size=2)
    nonlin = lasagne.nonlinearities.rectify
    network = lasagne.layers.DenseLayer(network, width, nonlinearity=nonlin)
    # Output layer:
    relu = lasagne.nonlinearities.rectify
    network = lasagne.layers.DenseLayer(network, 60, nonlinearity=relu)
    return network

File "/Users/adityanagarajan/anaconda/lib/python2.7/site-packages/lasagne/layers/dense.py", line 63, in __init__ super(DenseLayer, self).__init__(incoming, **kwargs) File "/Users/adityanagarajan/anaconda/lib/python2.7/site-packages/lasagne/layers/base.py", line 35, in __init__ self.input_shape = incoming.output_shape File "/Users/adityanagarajan/anaconda/lib/python2.7/site-packages/lasagne/layers/base.py", line 49, in output_shape return self.get_output_shape_for(self.input_shape) File "/Users/adityanagarajan/anaconda/lib/python2.7/site-packages/lasagne/layers/conv.py", line 237, in get_output_shape_for output_length = conv_output_length(input_shape[2], IndexError: tuple index out of range

o-90
  • 17,045
  • 10
  • 39
  • 63
Adi
  • 13
  • 2

1 Answers1

2

The error is telling you the answer:

line 237, in get_output_shape_for output_length = conv_output_length(input_shape[2], IndexError: tuple index out of range

If you check the source, on line 237, a tuple is trying to be indexed that doesn't have 3 elements in it. If you reshape your data it should resolve the error, try (None, 1, 119) as your input.

o-90
  • 17,045
  • 10
  • 39
  • 63
  • Yeah I tried that but my `input_var` is a Theano tensor `input_var = T.dmatrix('inputs') `, so I get an error when I use `(None,1,119)`, `ValueError: shape has 3 dimensions, but variable has 2`. I am wondering why the tuple must have a length of 3(`input_shape[2]`) for a 1D convolution. – Adi Nov 02 '15 at 17:07
  • 1
    Ya you need to change your input_var to `T.tensor3()`. Theano doesn't have a 1D convolution. The guys at Lasagne wrote extensions to "fake" them. The default conv function for `Conv1DLayer()` is `conv1d_mc0()`; you can see its implementation [here](https://github.com/Lasagne/Lasagne/blob/master/lasagne/theano_extensions/conv.py). A 2D-conv has a 4D input: (batch_size, channel, height, width). So a 1D-conv has input (batch_size, channel, length). You are inputing (batch_size, length) which is a 0D-conv (which is not even a thing and doesn't make sense). – o-90 Nov 02 '15 at 18:17
  • Thank you! used the T.tensor3() and reshaped to (num_examples,1,119) and works fine. – Adi Nov 02 '15 at 19:50