Theano 'Expected an array-like object, but found a Variable': Using scan & categorical_crossentropy

北城以北 提交于 2019-12-11 06:37:30

问题


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!


回答1:


I found my problem, it's really a stupid one.

loss = loss_cal(answers, predictions)

This is wrong, as predictions is the theano matrix, I should have been using 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)

It works now with loss = loss_cal(answers, pred) Thanks anyway



来源:https://stackoverflow.com/questions/43414306/theano-expected-an-array-like-object-but-found-a-variable-using-scan-categ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!