Search code examples
pythontheano

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


I'm a new guy in python, I'm coding theano these days and suffered a problem that bothered me many days, my code is like follows:

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]

what I am trying to do is assigning value based on string1 and string2 to some cell in dis_matrix, I use a condition T.switch(T.eq(string1[a_location[0]-1], string2[a_location[1]-1]) to decide value of temp, so that I can put temp into dis_matrix, but when I ran this code, I got error:

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

It seems that T.switch returns not a scalar but a 1-dim vector to me, if I change dis_matrix=T.inc_subtensor(dis_matrix[a_location[0], a_location[1]], temp) to dis_matrix=T.inc_subtensor(dis_matrix[a_location[0], a_location[1]], 0), this code works well, so I think it must be somewhere T.switch gets wrong.

Could any help me? Thanks a lot!


Solution

  • I have not verified this proposition by writing and running some code but the problem is probably that string1[a_location[0]-1] and/or string2[a_location[1]-1] are not scalars and are actually vectors (maybe vectors with only one entry). Consequently the results of T.eq and T.switch are also vectors (with the true/false scalar values in T.switch being broadcasted). If temp is a vector and dis_matrix[a_location[0], a_location[1]] is a scalar then inc_subtensor will probably yield the error you see.