TPU friendly version of tf.where(cond) with fixed length and padding. This is a wrapper around tf.where(cond) that returns the coordinates of the True elements of cond (case where x and y are None). This version, however, returns a fixed length tensor of coordinates, determined by `length`.
(condition, length)
| 99 | |
| 100 | |
| 101 | def padded_where(condition, length): |
| 102 | """TPU friendly version of tf.where(cond) with fixed length and padding. |
| 103 | |
| 104 | This is a wrapper around tf.where(cond) that returns the coordinates of the |
| 105 | True elements of cond (case where x and y are None). This version, however, |
| 106 | returns a fixed length tensor of coordinates, determined by `length`. If the |
| 107 | number of True elements in `condition` is less than `length`, then the |
| 108 | returned tensor is right-padded with zeros. Otherwise, the returned tensor is |
| 109 | truncated to `length` size. |
| 110 | |
| 111 | Args: |
| 112 | condition: tf.Tensor of type boolean; any shape. |
| 113 | length: Length of (last dimension of) the returned tensor. |
| 114 | |
| 115 | Returns: |
| 116 | Two tensors: |
| 117 | - a tensor of type int32, with same shape as `condition`, representing |
| 118 | coordinates of the last dimension of `condition` tensor where values are |
| 119 | True. |
| 120 | - a mask tensor of type int32 with 1s in valid indices of the first tensor, |
| 121 | and 0s for padded indices. |
| 122 | """ |
| 123 | condition_shape = shape(condition) |
| 124 | n = condition_shape[-1] |
| 125 | |
| 126 | # Build a tensor that counts indices from 0 to length of condition. |
| 127 | ixs = tf.broadcast_to(tf.range(n, dtype=tf.int32), condition_shape) |
| 128 | |
| 129 | # Build tensor where True condition values get their index value or |
| 130 | # n (== len(condition)) otherwise. |
| 131 | ixs = tf.where(condition, ixs, tf.ones_like(condition, dtype=tf.int32) * n) |
| 132 | |
| 133 | # Sort indices (so that indices for False values == n, will be placed last), |
| 134 | # and get the desired number of entries, truncating by `length`. |
| 135 | ixs = tf.sort(ixs)[Ellipsis, 0:length] |
| 136 | |
| 137 | # For first tensor, zero-out values == n. For second tensor, put 1s where |
| 138 | # values are < n, and 0s where values are == 0. |
| 139 | return tf.mod(ixs, n), (1 - tf.div(ixs, n)) |
| 140 | |
| 141 | |
| 142 | def shaped_py_func(func, inputs, types, shapes, stateful=True, name=None): |
nothing calls this directly
no test coverage detected
searching dependent graphs…