From 12940319ebf08e2fb1bb92b82e309c20759e44a4 Mon Sep 17 00:00:00 2001 From: Dan Smilkov Date: Mon, 12 Dec 2016 15:28:04 -0800 Subject: Fix t-sne bug (division by 0) when there are duplicate points. Change: 141818266 --- tensorflow/tensorboard/components/vz_projector/bh_tsne.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tensorflow/tensorboard/components/vz_projector/bh_tsne.ts b/tensorflow/tensorboard/components/vz_projector/bh_tsne.ts index 1d57dc720a..9d2df65f56 100644 --- a/tensorflow/tensorboard/components/vz_projector/bh_tsne.ts +++ b/tensorflow/tensorboard/components/vz_projector/bh_tsne.ts @@ -51,6 +51,9 @@ type AugmSPNode = SPNode&{numCells: number, yCell: number[], rCell: number}; * results. Recommended value mentioned in the paper is 0.8. */ const THETA = 0.8; + +const MIN_POSSIBLE_PROB = 1E-9; + // Variables used for memorizing the second random number since running // gaussRandom() generates two random numbers at the cost of 1 atomic // computation. This optimization results in 2X speed-up of the generator. @@ -159,6 +162,7 @@ function nearest2P( for (let k = 0; k < neighbors.length; ++k) { let neighbor = neighbors[k]; let pij = (i === neighbor.index) ? 0 : Math.exp(-neighbor.dist * beta); + pij = Math.max(pij, MIN_POSSIBLE_PROB); pRow[k] = pij; psum += pij; } @@ -433,7 +437,8 @@ export class TSNE { let squaredDistToCell = this.dist2(pointI, node.yCell); // Squared distance from point i to cell. if (node.children == null || - (node.rCell / Math.sqrt(squaredDistToCell) < THETA)) { + (squaredDistToCell > 0 && + node.rCell / Math.sqrt(squaredDistToCell) < THETA)) { let qijZ = 1 / (1 + squaredDistToCell); let dZ = node.numCells * qijZ; Z += dZ; -- cgit v1.2.3