將布爾掩碼應用於張量。
用法
tf.boolean_mask(
tensor, mask, axis=None, name='boolean_mask'
)參數
-
tensorN-D 張量。 -
maskK-D 布爾張量,K <= N 和 K 必須是靜態已知的。 -
axis一個 0-D int 張量,表示tensor中要屏蔽的軸。默認情況下,axis 為 0,這將屏蔽第一個維度。否則 K + 軸 -
name此操作的名稱(可選)。
返回
-
(N-K+1) 維張量由
tensor中的條目填充,對應於mask中的True值。
拋出
-
ValueError如果形狀不符合。
Numpy 等價物是 tensor[mask] 。
通常,0 < dim(mask) = K <= dim(tensor) 和 mask 的形狀必須與 tensor 的形狀的前 K 個維度匹配。然後我們有: boolean_mask(tensor, mask)[i, j1,...,jd] = tensor[i1,...,iK,j1,...,jd] 其中 (i1,...,iK) 是 mask 的第 i 個 True 條目(行主要順序)。 axis 可以與 mask 一起使用來指示要屏蔽的軸。在這種情況下,axis + dim(mask) <= dim(tensor) 和 mask 的形狀必須與 tensor 的形狀的第一個 axis + dim(mask) 尺寸相匹配。
另請參閱:tf.ragged.boolean_mask,它可以應用於密集和參差不齊的張量,如果您需要保留 tensor 的掩碼尺寸(而不是像 tf.boolean_mask 那樣將它們展平),可以使用它。
例子:
tensor = [0, 1, 2, 3] # 1-D example
mask = np.array([True, False, True, False])
tf.boolean_mask(tensor, mask)
<tf.Tensor:shape=(2,), dtype=int32, numpy=array([0, 2], dtype=int32)>
tensor = [[1, 2], [3, 4], [5, 6]] # 2-D example
mask = np.array([True, False, True])
tf.boolean_mask(tensor, mask)
<tf.Tensor:shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
[5, 6]], dtype=int32)>
例子:
# 2-D example
tensor = [[1, 2], [3, 4], [5, 6]]
mask = np.array([True, False, True])
boolean_mask(tensor, mask) # [[1, 2], [5, 6]]
相關用法
- Python tf.bitcast用法及代碼示例
- Python tf.broadcast_to用法及代碼示例
- Python tf.bitwise.bitwise_or用法及代碼示例
- Python tf.batch_to_space用法及代碼示例
- Python tf.broadcast_static_shape用法及代碼示例
- Python tf.broadcast_dynamic_shape用法及代碼示例
- Python tf.bitwise.bitwise_and用法及代碼示例
- Python tf.bitwise.bitwise_xor用法及代碼示例
- Python tf.bitwise.invert用法及代碼示例
- Python tf.bitwise.right_shift用法及代碼示例
- Python tf.bitwise.left_shift用法及代碼示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代碼示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代碼示例
- Python tf.compat.v1.data.TFRecordDataset.interleave用法及代碼示例
- Python tf.summary.scalar用法及代碼示例
- Python tf.linalg.LinearOperatorFullMatrix.matvec用法及代碼示例
- Python tf.linalg.LinearOperatorToeplitz.solve用法及代碼示例
- Python tf.raw_ops.TPUReplicatedInput用法及代碼示例
- Python tf.raw_ops.Bitcast用法及代碼示例
- Python tf.compat.v1.distributions.Bernoulli.cross_entropy用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.boolean_mask。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
