1

The objective is to obtain the subset of an array of elements through values provided in another array.

import theano
import theano.tensor as T

a = T.vector('X', dtype='int64')
b = T.vector('Y', dtype='int64')
c = a[b]
g = function([a,b],c)

x = np.array([5,3,2,3,4,6], dtype=int)
y = np.array([0,0,1,0,0,1], dtype=int)
print g(x,y)

This prints

[5 5 3 5 5 3]

instead of

[2 6]

How do I get the expected result?

Ébe Isaac
  • 11,563
  • 17
  • 64
  • 97

1 Answers1

1

try to use nonzero() function.

Example in your case:

import theano
import theano.tensor as T

a = T.vector('X', dtype='int64')
b = T.vector('Y', dtype='int64')
c = a[b.nonzero()]
g = function([a,b],c)

x = np.array([5,3,2,3,4,6], dtype=int)
y = np.array([0,0,1,0,0,1], dtype=int)
print g(x,y)

hope it helps

malioboro
  • 3,097
  • 4
  • 35
  • 55
  • Wonderful! Just what I was looking for! Usually, I'll use `astype(bool)` for `numpy` arrays, but was worried as I couldn't do this for tensors. I also just observed that `nonzero()` works for `numpy` too. Thanks a bunch. This problem was driving me crazy. – Ébe Isaac Apr 08 '16 at 14:16