我是theano的初学者,我正在寻找一种循环Tensor的方法。 我可以使用numpy数组开发功能,但我的Theano代码不起作用。
numpy:代码
a = np.asarray([[0.1,0.5,0.7,0.9,0,1],
[0.7,0.5,0.3,0.9,0,7]])
t = np.asarray([[0,1,1],
[0,1,1]])
def obj(predictions,targets):
pred = predictions.reshape(predictions.shape[0],3,2)
pred = np.vstack(np.argmax(pred[i],axis=1) for i in range(predictions.shape[0]))
som = np.abs(targets - pred)
return np.sum(som)
print obj(a,t)
我尝试过但不起作用
def obj(predictions, targets):
pred = predictions.reshape([T.shape(predictions)[0],3,2])
X = T.matrix("X")
results = theano.scan(lambda x_i: np.vstack(T.argmax(x_i,axis=1)),sequences=[X])
compute_elementwise = theano.function(inputs=[X], outputs=[results])
pred = compute_elementwise(pred)
som = T.abs_(targets-pred)
return np.sum(som)/T.shape(predictions)[0]