gpt4 book ai didi

python - 类型错误 : rank mismatch between coding and true distributions

转载 作者:太空宇宙 更新时间:2023-11-03 11:26:01 24 4
gpt4 key购买 nike

我正在尝试适应 Lasagne tutorial到 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

最佳答案

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

不是batchsize,seqlen

关于python - 类型错误 : rank mismatch between coding and true distributions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33651732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com