4

我正在尝试将Lasagne 教程改编为 LSTM 模型。这就是我的代码是 atm 的方式:

def build_lstm(input_var=None):
    num_inputs, num_units, num_classes = 4978, 300, 127
    l_inp = lasagne.layers.InputLayer((None, None, num_inputs))
    batchsize, seqlen, _ = l_inp.input_var.shape
    l_lstm = lasagne.layers.LSTMLayer(l_inp, num_units=num_units)
    l_shp = lasagne.layers.ReshapeLayer(l_lstm, (-1, num_units))
    l_dense = lasagne.layers.DenseLayer(l_shp, num_units=num_classes)
    l_out = lasagne.layers.ReshapeLayer(l_dense, (batchsize, seqlen, num_classes))

    return l_out

train_in, test_in, train_out, test_out = build_dataset()

input_var = T.tensor4('inputs')
target_var = T.ivector('targets')

myLSTMNetwork = build_lstm()

#Loss evaluation
prediction = lasagne.layers.get_output(myLSTMNetwork)
loss = lasagne.objectives.categorical_crossentropy(prediction, target_var)
loss = loss.mean()

我可以毫无错误地构建我的模型。但是在插入与损失评估相关的代码后,我得到了这个错误:

File "code.py", line 110, in build_lstm
    loss = lasagne.objectives.categorical_crossentropy(prediction, target_var)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lasagne/objectives.py", line 146, in categorical_crossentropy
    return theano.tensor.nnet.categorical_crossentropy(predictions, targets)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/theano/tensor/nnet/nnet.py", line 1906, in categorical_crossentropy
    raise TypeError('rank mismatch between coding and true distributions')
TypeError: rank mismatch between coding and true distributions
4

1 回答 1

2

我的输出层应该有batchsize * seqlen作为参数。

不是批量大小,seqlen

于 2015-11-11T17:56:26.297 回答