TypeError: rank mismatch between encoding and true distributions

I am trying to adapt a climbing tutorial to the LSTM model. This is how my atm code is:

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()

I can build my model without errors. But after pasting the code related to the Loss score, I get this error:

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
source share
1 answer

My output layer should have batchsize * seqlen as a parameter.

Do not batchsize, seqlen .

+2
source

Source: https://habr.com/ru/post/1615411/


All Articles