I'm trying to sum multiple loss in theano but I can't make it work. I'm using the categorical crossentroy.
Here is my code:
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)
The error I'm getting is
Expected an array-like object, but found a Variable:
TypeError: ('Bad input argument to theano function with name "main.py:89" at index1(0-based)', Expected an array-like object but found a Variable: maybe you are trying to call a function on a (possibly shared) variable instead of a numeric array?
I don't get why my code doesn't work, can someone explain it to me? Thanks a lot!