Python TypeError:尝试用一维值递增0维子传感器

Python TypeError:尝试用一维值递增0维子传感器,python,theano,Python,Theano,我是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(

我是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_矩阵中的某个单元格赋值,我使用条件T.switch(T.eq(string1[a_location[0]-1],string2[a_location[1]-1])来决定temp的值,这样我就可以将temp放入dis_矩阵,但当我运行此代码时,我得到了错误:

TypeError:尝试用一维值递增0维子传感器

如果我将
dis_矩阵=T.inc_子传感器(dis_矩阵[a_位置[0],a_位置[1]],temp)更改为
dis_矩阵=T.inc_子传感器(dis_矩阵[a_位置[0],a_位置[1]],0),则
T.switch
似乎不会向我返回一个标量,而是一个一维向量
,此代码运行良好,因此我认为T.switch一定是在某个地方出错了


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

我还没有通过编写和运行一些代码来验证这个命题,但问题可能是
string1[a_位置[0]-1]
和/或
string2[a_位置[1]-1]
不是标量,实际上是向量(可能只有一个条目的向量)因此,
T.eq
T.switch
的结果也是向量(广播
T.switch
中的真/假标量值)。如果
temp
是向量且
disu矩阵[a_位置[0],a_位置[1]]
是一个标量,那么
inc_subsensor
可能会产生您看到的错误。

这不是有效的python代码。如果您可以将其编写为完全自给自足且可以通过粘贴到IPython控制台来运行,那就太好了。谢谢您的提醒,我已经解决了这个问题,我得到了string2 wron的维度g、 无论如何,谢谢你的帮助!谢谢伦肖!我检查了我的代码,发现问题正是你所说的,我犯了一个错误,string2的尺寸不正确。现在我纠正了它,它工作得很好。起初,我想T.switch可能返回一个向量,而不是0或10。