Differences between Assigning Indexes in Numpy and Theano set_subtensor ()

I'm trying to do an index assignment in Theano using set_subtensor (), but it gives different results for assigning a Numpy index. Am I doing something wrong, or is it the difference in how the set_subtensor and Numpy index functions work?

What I want to do:

X = np.zeros((2, 2))
X[[[0, 1], [0, 1]]] = np.array([1, 2])

X is now:
[[ 1.  0.]
 [ 0.  2.]]

Trying to do the same in Theano:

X = theano.shared(value=np.zeros((2, 2)))
X = T.set_subtensor(X[[[0, 1], [0, 1]]], np.array([1, 2]))
X.eval()

Causes this error

ValueError: array is not broadcastable to correct shape
+2
source share
1 answer

This emphasizes the subtle difference between numpy and Theano, but you can easily work with ease.

Extended indexing can be included in numpy using a list of items or a tuple of items. In Theano, you can only use a tuple of positions.

So changing

X = T.set_subtensor(X[[[0, 1], [0, 1]]], np.array([1, 2]))

to

X = T.set_subtensor(X[([0, 1], [0, 1])], np.array([1, 2]))

solves the problem in Theano.

numpy,

X[[[0, 1], [0, 1]]] = np.array([1, 2])

X[([0, 1], [0, 1])] = np.array([1, 2])
+2

All Articles