python - Theano 'Expected an array-like object, but found a Variable': Using scan & categorical_crossentropy -
i'm trying sum multiple loss in theano can't make work. i'm using categorical crossentroy.
here code:
import numpy np import theano import theano.tensor 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 generating random data see if can make code work max_nbr = 5 pred = [] in range(0, max_nbr): temp = np.ones(8) temp[i] = temp[i] + 5 temp = temp/sum(temp) pred.append(temp) answers = [] in range(0, max_nbr): answers.append(pred[i].argmax()) loss = loss_cal(answers, predictions) print(loss)
the error i'm getting
expected array-like object, found variable: typeerror: ('bad input argument theano function name "main.py:89" @ index1(0-based)', expected array-like object found variable: maybe trying call function on (possibly shared) variable instead of numeric array?
i don't why code doesn't work, can explain me? lot!
i found problem, it's stupid one.
loss = loss_cal(answers, predictions)
this wrong, predictions theano matrix, should have been using pred
.
pred = [] in range(0, max_nbr): temp = np.ones(8) temp[i] = temp[i] + 5 temp = temp/sum(temp) pred.append(temp)
it works loss = loss_cal(answers, pred)
thanks anyway
Comments
Post a Comment