我试图在theano中总结多次失利,但我无法使其发挥作用。 我使用了分类的crossentroy。
这是我的代码:
RegexOptions.Singleline
我得到的错误是
import numpy as np
import theano
import theano.tensor as T
answers = T.ivector()
temp = T.scalar()
predictions = T.matrix()
def loss_acc(curr_ans,curr_pred, loss):
temp= T.nnet.categorical_crossentropy(curr_pred.dimshuffle('x',0), T.stack([curr_ans]))[0]
return temp + loss
outputs, updates = theano.scan(fn = loss_acc,
sequences = [answers, predictions],
outputs_info = [np.float64(0.0)],
n_steps = 5)
loss = outputs[-1]
loss_cal = theano.function(inputs = [answers, predictions], outputs = [loss])
#Here I'm just generating some random data to see if I can make the code work
max_nbr = 5
pred = []
for i in range(0, max_nbr):
temp = np.ones(8)
temp[i] = temp[i] + 5
temp = temp/sum(temp)
pred.append(temp)
answers = []
for i in range(0, max_nbr):
answers.append(pred[i].argmax())
loss = loss_cal(answers, predictions)
print(loss)
我不知道为什么我的代码不起作用,有人可以向我解释一下吗?非常感谢!
答案 0 :(得分:0)
我发现了我的问题,这真的是一个愚蠢的问题。
loss = loss_cal(answers, predictions)
这是错误的,因为预测是theano矩阵,我本应该使用pred
。
pred = []
for i in range(0, max_nbr):
temp = np.ones(8)
temp[i] = temp[i] + 5
temp = temp/sum(temp)
pred.append(temp)
现在可以使用了
loss = loss_cal(answers, pred)
无论如何,谢谢