tensorflow里的一个函数,boolean_mask(a,b) 将使a (m维)矩阵仅保留与b中“True”元素同下标的部分。在做目标检测(YOLO)时常常用到。
“”"Apply boolean mask to tensor. Numpy equivalent is tensor[mask]
.
# 1-D example
tensor = [0, 1, 2, 3]
mask = np.array([True, False, True, False])
boolean_mask(tensor, mask) # [0, 2]
In general, 0 < dim(mask) = K <= dim(tensor)
, and mask
's shape must match
the first K dimensions of tensor
's shape.
We then have:
boolean_mask(tensor, mask)[i, j1,...,jd] = tensor[i1,...,iK,j1,...,jd]
where (i1,...,iK)
is the ith True
entry of mask
(row-major order).
The axis
could be used with mask
to indicate the axis to mask from.
In that case, axis + dim(mask) <= dim(tensor)
and mask
's shape must match
the first axis + dim(mask)
dimensions of tensor
's shape.
以上是源码里的解释,没看懂。下面自己看例子。
Mask的维度可以小于等于tensor的维度。但必须和tensor的维度对齐。比如tensor是3维的,分别是(3,4,2)。则mask的维度可以是3~1维,长度必须是(3,4,2),(3,4)和(3,)。
当mask和tensor的维度相同时,输出1维矩阵。
当mask比tensor少一维时,输出2维矩阵。
当mask比bensor少两维时,输出3维矩阵。
即输出的维度= tensor的维度 – mask的维度+ 1
注意这里的对应关系,mask的第1,2个维度对应了tensor的第1,2个维度。这种对应不分行列。