Compute set intersection of elements in last dimension of `a` and `b`. All but the last dimension of `a` and `b` must match. Example: ```python import tensorflow as tf import collections # Represent the following array of sets as a sparse tensor: # a = np.array([[{1, 2}, {3
(a, b, validate_indices=True)
| 136 | @tf_export( |
| 137 | "sets.intersection", v1=["sets.intersection", "sets.set_intersection"]) |
| 138 | def set_intersection(a, b, validate_indices=True): |
| 139 | """Compute set intersection of elements in last dimension of `a` and `b`. |
| 140 | |
| 141 | All but the last dimension of `a` and `b` must match. |
| 142 | |
| 143 | Example: |
| 144 | |
| 145 | ```python |
| 146 | import tensorflow as tf |
| 147 | import collections |
| 148 | |
| 149 | # Represent the following array of sets as a sparse tensor: |
| 150 | # a = np.array([[{1, 2}, {3}], [{4}, {5, 6}]]) |
| 151 | a = collections.OrderedDict([ |
| 152 | ((0, 0, 0), 1), |
| 153 | ((0, 0, 1), 2), |
| 154 | ((0, 1, 0), 3), |
| 155 | ((1, 0, 0), 4), |
| 156 | ((1, 1, 0), 5), |
| 157 | ((1, 1, 1), 6), |
| 158 | ]) |
| 159 | a = tf.SparseTensor(list(a.keys()), list(a.values()), dense_shape=[2,2,2]) |
| 160 | |
| 161 | # b = np.array([[{1}, {}], [{4}, {5, 6, 7, 8}]]) |
| 162 | b = collections.OrderedDict([ |
| 163 | ((0, 0, 0), 1), |
| 164 | ((1, 0, 0), 4), |
| 165 | ((1, 1, 0), 5), |
| 166 | ((1, 1, 1), 6), |
| 167 | ((1, 1, 2), 7), |
| 168 | ((1, 1, 3), 8), |
| 169 | ]) |
| 170 | b = tf.SparseTensor(list(b.keys()), list(b.values()), dense_shape=[2, 2, 4]) |
| 171 | |
| 172 | # `tf.sets.intersection` is applied to each aligned pair of sets. |
| 173 | tf.sets.intersection(a, b) |
| 174 | |
| 175 | # The result will be equivalent to either of: |
| 176 | # |
| 177 | # np.array([[{1}, {}], [{4}, {5, 6}]]) |
| 178 | # |
| 179 | # collections.OrderedDict([ |
| 180 | # ((0, 0, 0), 1), |
| 181 | # ((1, 0, 0), 4), |
| 182 | # ((1, 1, 0), 5), |
| 183 | # ((1, 1, 1), 6), |
| 184 | # ]) |
| 185 | ``` |
| 186 | |
| 187 | Args: |
| 188 | a: `Tensor` or `SparseTensor` of the same type as `b`. If sparse, indices |
| 189 | must be sorted in row-major order. |
| 190 | b: `Tensor` or `SparseTensor` of the same type as `a`. If sparse, indices |
| 191 | must be sorted in row-major order. |
| 192 | validate_indices: Whether to validate the order and range of sparse indices |
| 193 | in `a` and `b`. |
| 194 | |
| 195 | Returns: |
no test coverage detected