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.