5

I'm using a cost function in Theano which involves a regularizer term that requires me to compute this term:

T.sum(c / self.squared_euclidean_distances)

as some values of self.squared_euclidean_distances might be zero this produces Nan values. How can i work around this problem? I tried to use T.isinf but were not successful. One solution would be to remove zeros in self.squared_euclidean_distances into a small number or replace infinite numbers in T.sum(c / self.squared_euclidean_distances) to zero. I just don't know how to replace those values in Theano.

Ash
  • 3,428
  • 1
  • 34
  • 44

1 Answers1

9

Take a look at T.switch. You could do for example

T.switch(T.eq(self.squared_euclidean_distances, 0), 0, c / self.squared_euclidean_distances)

(Or, upstream, you make sure that you never compare a vector with itself using squared euclidean distance.)

eickenberg
  • 14,152
  • 1
  • 48
  • 52
  • 2
    I'm using your solution to find the pairwise distances of rows of a matrix without loops so i can't use the upstream solution easily I guess. – Ash Oct 30 '14 at 00:31
  • 1
    There was a rejected edit on this that should have been approved, changing `==` to `T.eq`. I will do this change myself, because I don't know how to reapprove a falsely rejected edit. – eickenberg Mar 17 '16 at 12:01