tf.boolean_mask
时间: 2023-08-05 19:09:26 浏览: 97
tf.boolean_mask is a function in TensorFlow that allows you to apply a boolean mask to an input tensor and return a new tensor containing only the elements that correspond to True values in the mask. The mask is a boolean tensor with the same shape as the input tensor, and it determines which elements to keep in the output tensor.
Here's an example usage of tf.boolean_mask:
```python
import tensorflow as tf
input_tensor = tf.constant([1, 2, 3, 4, 5])
mask = tf.constant([True, False, True, False, True])
output_tensor = tf.boolean_mask(input_tensor, mask)
print(output_tensor)
```
Output:
```
[1 3 5]
```
In this example, the boolean mask `[True, False, True, False, True]` selects the elements in the input tensor `[1, 2, 3, 4, 5]` at positions where the mask is True. The resulting output tensor contains only the selected elements `[1, 3, 5]`.
阅读全文