无法弄清楚我的Lasagne LSTM的问题

时间:2023-01-26 21:10:21

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.

如果有人有任何见解,我很好奇。即使你无法弄清楚问题我怎么能开始调试它。我必须说,我在theano中并不坚强。

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

输入数据是一个numpy张量的形状(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.

2 个解决方案

#1


1  

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

ResultsBatch可能包含1到10之间的数字吗?

If so, you could try testing with

如果是这样,您可以尝试使用

train_fn(TextBatch,ResultsBatch - 1)

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

将目标转换为0,1,2,..,9而不是。

#2


1  

There are a few mistakes in this:

这有一些错误:

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

你的输入是一个向量。您的输出在批次中每个条目有10个单位:

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,):

您的输出形状为(150,10),您要与标签输入(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

您可能希望为标签使用(10,10)矩阵。这样您就可以使用10个类,批量大小为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.

您还需要更改网络,以便输出(10,10)形状的结果。你可以通过直接重塑到(10,某些东西)来重塑你的l_shp来实现这一点。还有很多其他选择。

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

顺便说一句,您可以使用'output_shape'属性查看图层形状。

#1


1  

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

ResultsBatch可能包含1到10之间的数字吗?

If so, you could try testing with

如果是这样,您可以尝试使用

train_fn(TextBatch,ResultsBatch - 1)

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

将目标转换为0,1,2,..,9而不是。

#2


1  

There are a few mistakes in this:

这有一些错误:

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

你的输入是一个向量。您的输出在批次中每个条目有10个单位:

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,):

您的输出形状为(150,10),您要与标签输入(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

您可能希望为标签使用(10,10)矩阵。这样您就可以使用10个类,批量大小为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.

您还需要更改网络,以便输出(10,10)形状的结果。你可以通过直接重塑到(10,某些东西)来重塑你的l_shp来实现这一点。还有很多其他选择。

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

顺便说一句,您可以使用'output_shape'属性查看图层形状。