TypeError: Trying to increment a 0-dimensional subtensor with a 1-dimensional value

TypeError: Trying to increment a 0-dimensional subtensor with a 1-dimensional value

我是python的新人,这几天在写theano,遇到了一个困扰我很多天的问题,我的代码如下:

import theano.tensor as T

def compute_distance(location, dis_matrix, string1, string2, len1, lent):
def assign_distance(a_location, dis_matrix, string1, string2, len1, len2):
    temp = T.switch(T.eq(string1[a_location[0]-1], string2[a_location[1]-1]), 0, 10)
    dis_matrix=T.inc_subtensor(dis_matrix[a_location[0], a_location[1]], temp)
return dis_matrix, temp

result, updates = theano.scan(fn=assign_distance,
                              outputs_info=[dis_matrix, None],
                              sequences=location,
                              non_sequences=[string1, string2, len1, len2])
return result[0][-1], result[1]

我想做的是根据 string1 和 string2 将值分配给 dis_matrix 中的某个单元格,我使用条件 T.switch(T.eq(string1[a_location[0]-1], string2[a_location[1]-1]) 来决定temp的值,这样我就可以把temp放到dis_matrix,但是当我运行这段代码,我得到了错误:

TypeError: Trying to increment a 0-dimensional subtensor with a 1-dimensional value.

似乎T.switch returns对我来说不是标量而是1-dim矢量,如果我将dis_matrix=T.inc_subtensor(dis_matrix[a_location[0], a_location[1]], temp)更改为dis_matrix=T.inc_subtensor(dis_matrix[a_location[0], a_location[1]], 0),此代码运行良好,所以我认为它一定是在某个地方 T.switch 出错了。

有什么可以帮助我的吗?非常感谢!

我还没有通过编写和 运行 一些代码来验证这个命题,但问题可能是 string1[a_location[0]-1] and/or string2[a_location[1]-1] 不是标量,实际上是向量(可能只有一个条目的向量)。因此,T.eqT.switch 的结果也是向量(广播 T.switch 中的 true/false 标量值)。如果 temp 是向量而 dis_matrix[a_location[0], a_location[1]] 是标量,那么 inc_subtensor 可能会产生您看到的错误。