0

我很好奇是否有人有任何见解。即使您无法弄清楚问题,我该如何开始调试它。我必须说,我在theano方面并不强。

输入数据是形状为 (10,15,10) 的 numpy 张量

这里是。它在我刚刚将输入连接到密集层时运行。

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)

这是错误。这有点笨拙。

---------------------------------------------------------------------------
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.
4

2 回答 2

1

这其中有几个错误:

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

target_var = T.ivector('targets')
l_out = lasagne.layers.DenseLayer(l_shp, num_units=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)

您可能希望为标签使用 (10, 10) 矩阵。这样,您将使用 10 个类来处理 10 的批量大小

target_var = T.matrix('targets')

您还需要更改您的网络,使其输出 (10,10) 形状的结果。您可以通过直接重塑为 (10, something) 以不同方式重塑您的 l_shp 来做到这一点。还有很多其他选择。

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

于 2016-03-19T00:00:31.357 回答
1

ResultsBatch 是否可能包含从 1 到 10 的数字?

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

train_fn(TextBatch,ResultsBatch - 1)

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

于 2015-09-04T15:26:38.513 回答