How can a tensor be flipped in Theano?

theano

Solution

You can simply do `v[::-1].eval()`, or just `v[::-1]` in the middle of your computational graph.

Minimal example:

import numpy as np
import theano
from theano import tensor as T

X_values = np.arange(10).astype(theano.config.floatX)
X = T.shared(X_values, 'X')
print(X.eval())
print(X[::-1].eval())

See the section on indexing here for more details.

Problem

Given a tensor `v = t.vector()`, how can I flip it? For instance, `[1, 2, 3, 4, 5, 6]` flipped is `[6, 5, 4, 3, 2, 1]`.

Original source