Sample length from truncated Geo(p).
(start, end)
| 214 | return tf.reduce_any([wordpiece_boundary, all_stopword, length]) |
| 215 | |
| 216 | def sample(start, end): |
| 217 | """Sample length from truncated Geo(p).""" |
| 218 | # Sample from truncated geometric distribution. |
| 219 | geometric = lambda k: (1 - p)**(k - 1) * p |
| 220 | probs = np.array([geometric(k) for k in range(1, max_span_len + 1)]) |
| 221 | probs /= probs.sum() |
| 222 | length = tf.distributions.Categorical(probs=probs).sample() + 1 |
| 223 | |
| 224 | # Sample start uniformly. |
| 225 | max_offset = tf.maximum(1, seq_len - length + 1) |
| 226 | start = tf.random.uniform([], 0, max_offset, dtype=tf.int32) |
| 227 | end = start + length |
| 228 | |
| 229 | # Return span. |
| 230 | return [start, end] |
| 231 | |
| 232 | # Rejection sample. Start with dummy span variable. |
| 233 | start = tf.constant(0) |
nothing calls this directly
no test coverage detected
searching dependent graphs…