6

How can I index a matrix in Theano by a vector of indices?
More precisely, say that:

  • v has type theano.tensor.vector (e.g. [0,2])
  • A has type theano.tensor.matrix (e.g. [[1,0,0], [0,1,0], [0,0,1]])

The desired result is [[1,0,0], [0,0,1]].
I mention that my goal is to convert a list of indices into a matrix of one-hot row vectors, where the indices indicate the hot position. My initial attempt was to let A = theano.tensor.eye and index it using the vector of indices.

John Jaques
  • 560
  • 4
  • 17

1 Answers1

5

You can do:

A[v]

It will do what you want.

nouiz
  • 5,071
  • 25
  • 21
  • I have tried this already, but I get an error message. I define *v = theano.tensor.vector()*, *A = theano.tensor.matrix()* and *indexed = A[v]*. The error message is the following: *TypeError: index must be integers*. – John Jaques Jun 13 '14 at 10:46
  • 1
    the list v, must be integers. Can you cast to to int like this: theano.tensor.cast(v, 'int64'). – nouiz Jun 13 '14 at 14:34
  • v = theano.tensor.lvector() gives you an integer 64 bit vector without the need to cast. Check types list here: http://deeplearning.net/software/theano/library/tensor/basic.html#all-fully-typed-constructors – JStrahl Dec 28 '15 at 13:47