Performs binary search to find suitable precision.
(self, dist, target_entropy)
| 99 | return affines |
| 100 | |
| 101 | def _binary_search(self, dist, target_entropy): |
| 102 | """Performs binary search to find suitable precision.""" |
| 103 | precision_min = 0 |
| 104 | precision_max = 1.0e15 |
| 105 | precision = 1.0e5 |
| 106 | |
| 107 | for _ in range(self.perplexity_tries): |
| 108 | denom = np.sum(np.exp(-dist[dist > 0.0] / precision)) |
| 109 | beta = np.exp(-dist / precision) / denom |
| 110 | |
| 111 | # Exclude zeros |
| 112 | g_beta = beta[beta > 0.0] |
| 113 | entropy = -np.sum(g_beta * np.log2(g_beta)) |
| 114 | |
| 115 | error = entropy - target_entropy |
| 116 | |
| 117 | if error > 0: |
| 118 | # Decrease precision |
| 119 | precision_max = precision |
| 120 | precision = (precision + precision_min) / 2.0 |
| 121 | else: |
| 122 | # Increase precision |
| 123 | precision_min = precision |
| 124 | precision = (precision + precision_max) / 2.0 |
| 125 | |
| 126 | if np.abs(error) < self.tol: |
| 127 | break |
| 128 | |
| 129 | return beta |
| 130 | |
| 131 | def _q_distribution(self, D): |
| 132 | """Computes Student t-distribution.""" |
no outgoing calls
no test coverage detected