Elliott Miller
Elliott Miller

Reputation: 363

Can't figure out the issue with my Lasagne LSTM

I'm curious if anyone has any insights. Even if you can't figure out the issue how can I begin to debug it. I must say, I am not strong in theano.

The input data is a numpy tensor of shape (10,15,10)

Here it is. It ran when I just hooked up the input to the dense layer.

def MakeSentimentLSTM(input_var):
    l_in = lasagne.layers.InputLayer(shape=(10,15,10),
                                     input_var=input_var)
    l_lstm = lasagne.layers.LSTMLayer(l_in, num_units=10,peepholes=False)
    l_shp =lasagne.layers.ReshapeLayer(l_lstm, (10*15, 10))
    l_out = lasagne.layers.DenseLayer(l_shp, num_units=10)

    return l_out
# Prepare Theano variables for inputs and targets
input_var = T.tensor3('inputs')
target_var = T.ivector('targets')
# Create neural network model
network = MakeSentimentLSTM(input_var)
#The Network output or prediction
prediction = lasagne.layers.get_output(network)
# Set the Error Function
loss = lasagne.objectives.categorical_crossentropy(prediction, target_var)
loss = loss.mean()
#pool the numpy shared variables
params = lasagne.layers.get_all_params(network, trainable=True)

#using adagrad to train the weights
updates = lasagne.updates.adagrad(
    loss, params)
#Initiate training. 
#Just doing a single pass and not worring about epochs and mini batches now
train_fn = theano.function([input_var, target_var], loss, updates=updates)

train_fn(TextBatch,ResultsBatch)

Here is the error. It is kind of a doozy.

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-73-49980235fbb7> in <module>()
----> 1 train_fn(TextBatch,ResultsBatch)

C:\Users\ellmi_000\Anaconda\lib\site-packages\theano\compile\function_module.pyc in __call__(self, *args, **kwargs)
    616                         self.fn.nodes[self.fn.position_of_error],
    617                         self.fn.thunks[self.fn.position_of_error],
--> 618                         storage_map=self.fn.storage_map)
    619                 else:
    620                     # For the c linker We don't have access from

C:\Users\ellmi_000\Anaconda\lib\site-packages\theano\gof\link.pyc in raise_with_op(node, thunk, exc_info, storage_map)
    295     exc_value = exc_type(str(exc_value) + detailed_err_msg +
    296                          '\n' + '\n'.join(hints))
--> 297     reraise(exc_type, exc_value, exc_trace)
    298 
    299 

C:\Users\ellmi_000\Anaconda\lib\site-packages\theano\compile\function_module.pyc in __call__(self, *args, **kwargs)
    605         t0_fn = time.time()
    606         try:
--> 607             outputs = self.fn()
    608         except Exception:
    609             if hasattr(self.fn, 'position_of_error'):

C:\Users\ellmi_000\Anaconda\lib\site-packages\theano\gof\op.pyc in rval(p, i, o, n)
    759             # default arguments are stored in the closure of `rval`
    760             def rval(p=p, i=node_input_storage, o=node_output_storage, n=node):
--> 761                 r = p(n, [x[0] for x in i], o)
    762                 for o in node.outputs:
    763                     compute_map[o][0] = True

C:\Users\ellmi_000\Anaconda\lib\site-packages\theano\tensor\nnet\nnet.pyc in perform(self, node, inp, out)
   1306         y = numpy.zeros_like(coding[:, 0])
   1307         for i in xrange(len(y)):
-> 1308             y[i] = -numpy.log(coding[i, one_of_n[i]])
   1309         y_out[0] = y
   1310 

IndexError: index 10 is out of bounds for axis 0 with size 10
Apply node that caused the error: CrossentropyCategorical1Hot(Elemwise{Composite{(i0 * (Abs(i1) + i2 + i3))}}[(0, 2)].0, targets)
Toposort index: 206
Inputs types: [TensorType(float64, matrix), TensorType(int32, vector)]
Inputs shapes: [(150L, 10L), (10L,)]
Inputs strides: [(80L, 8L), (4L,)]
Inputs values: ['not shown', 'not shown']
Outputs clients: [[Sum{acc_dtype=float64}(CrossentropyCategorical1Hot.0), Shape_i{0}(CrossentropyCategorical1Hot.0)]]

Backtrace when the node is created:
  File "C:\Users\ellmi_000\Anaconda\lib\site-packages\lasagne\objectives.py", line 129, in categorical_crossentropy
    return theano.tensor.nnet.categorical_crossentropy(predictions, targets)

HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.

Upvotes: 0

Views: 1489

Answers (2)

karsten
karsten

Reputation: 101

There are a few mistakes in this:

Your input is a vector. Your output has 10 units per entry in your batch:

target_var = T.ivector('targets')
l_out = lasagne.layers.DenseLayer(l_shp, num_units=10)

Your output shape is (150, 10) which you want to compare to your label input (10,):

l_shp =lasagne.layers.ReshapeLayer(l_lstm, (10*15, 10))
print 'l_shp shape:', l_shp.output_shape
l_out = lasagne.layers.DenseLayer(l_shp, num_units=10)

You probably want to use a (10, 10) matrix for your labels. This way you are using 10 classes for your batch size of 10

target_var = T.matrix('targets')

You also need to change your network so that it outputs a (10,10) shaped result. You can do this by reshaping your l_shp differently by reshaping directly to (10, something). There are lots of other options.

Btw, you can look at layer shapes with their 'output_shape' property.

Upvotes: 1

Peter de Rivaz
Peter de Rivaz

Reputation: 33519

Is it possible that ResultsBatch contains numbers from 1 to 10?

If so, you could try testing with

train_fn(TextBatch,ResultsBatch - 1)

to convert the targets to 0,1,2,..,9 instead.

Upvotes: 1

Related Questions