0

My goal is to invert the values in a Theano tensor element-wise. For instance, I want to turn [4, 5, 10] into [0.25, 0.2, 0.1]. If there is a zero element, I want to keep it unchanged (e.g. [0, 4, 5, 10] should be turned into [0, 0.25, 0.2, 0.1]). What is the most elegant way of doing this?

My solution was to add a very small value to all the elements:

v = t.vector()
result = 1 / (v + 0.000001)

I am aware that I could use a scan to iterate over the elements and check individually whether they can be inverted, but I want my code to look as mathsy as possible.

John Jaques
  • 560
  • 4
  • 17
  • 1
    See http://stackoverflow.com/questions/20293614/divide-one-numpy-array-by-another-only-where-both-arrays-are-non-zero for a couple of approaches. – mtrw Jul 03 '14 at 14:12
  • I don't get how your solution is valid. – user189 Jul 03 '14 at 14:54
  • What does "valid" mean? Are you referring to correctness? Adding 0.00001 to each element in the vector will eliminate all zeros, and there will be no division by zero. Of course, the result will only be approximate for the rest of the values. – John Jaques Jul 03 '14 at 20:56

1 Answers1

1

You can get this using T.sgn() since the sign of 0 is 0, though it will probably still fail for very, very small values near 0

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')
Y = (X ** (-1 * abs(T.sgn(X)))) * abs(T.sgn(X))
print(Y.eval())

I think what user189 was saying is that mathematically this is not really correct - the result should go to infinity as the denominator approaches 0. What is the application where you need this? You can always test for T.isinf and T.isnan to try and catch numerical problems without resorting to tricks, and raising an exception is probably a cleaner approach than altering the math.

Kyle Kastner
  • 1,008
  • 8
  • 7