I can't seem to find a solution for this. Given two theano tensors a and b, I want to find the indices of elements in b within the tensor a. This example will help, say a = [1, 5, 10, 17, 23, 39] and b = [1, 10, 39], I want the result to be the indices of the b values in tensor a, i.e. [0, 2, 5].
After spending some time, I thought the best way would be to use scan; here is my shot at the minimal example.
def getIndices(b_i, b_v, ar):
pI_subtensor = pI[b_i]
return T.set_subtensor(pI_subtensor, np.where(ar == b_v)[0])
ar = T.ivector()
b = T.ivector()
pI = T.zeros_like(b)
result, updates = theano.scan(fn=getIndices,
outputs_info=None,
sequences=[T.arange(b.shape[0], dtype='int32'), b],
non_sequences=ar)
get_proposal_indices = theano.function([b, ar], outputs=result)
d = get_proposal_indices( np.asarray([1, 10, 39], dtype=np.int32), np.asarray([1, 5, 10, 17, 23, 39], dtype=np.int32) )
I am getting the error:
TypeError: Trying to increment a 0-dimensional subtensor with a 1-dimensional value.
in the return statement line. Further, the output needs to be a single tensor of shape b and I am not sure if this would get the desired result. Any suggestion would be helpful.