Tensorflow Learning Notes-tf.boolean_mask()

  • tf.boolean_mask(tensor,mask,name="boolean_mask",axis=None)
  • Apply boolean mask to tensor,Numpy equivalent is 'tensor[mask]'
  • tensor: N-D tensor
  • mask: K-D boolean mask,K<=N and K must be known statically.
  • name: the name of this operation.
  • axis: 0-D tensor,represrnting the axis in 'tensor' to mask from.

Bofore we understand what this funtion actually do,we should know the boolean_mask's mean.mask is a piece of cloth,which you wear your face so that you can look like someone or something else.In tensorflow,boolean_mask has a similar work like "mask",this mask is a boolean K-D tesnor,if the value in "mask" is True,the corresponding value in "tensor" would be reserved,otherwise,it would be deserted.Raise a simple example: if we have a tensor [1,2,3,4],meanwhile we have a boolean mask [1,0,0,1] or [True,False,False,True],and the returns after boolean_mask is [1,4].In conclusion,tf.boolean_mask() really like a mask,it will hide the False element or tensor in "inputs",and reserve the True value in "inputs".There is an example program to explain this function as follow:

import tensorflow as tf
a=[[1,1,1],[2,2,2],[3,3,3]]#==> a (3,3) tensor
mask=[True,False,True]
c=tf.boolean_mask(a,mask,axis=0)
d=tf.boolean_mask(a,mask,axis=1)

sess=tf.Session()
print(sess.run(c)) #masks tensor "a" in first dimention
print(sess.run(d)) #masks tensor "a" in second dimention

Result:
c:
 [[1 1 1]
 [3 3 3]]
d:
 [[1 1]
 [2 2]
 [3 3]]

As is shown in front code,tensor "a"  has  been masked in each dimention.As a result of tensor a is a (3,3) 2D tensor,when axis=0,the mask will perform in first dimention,it is 3,and if axis=1,it will perform in the second dimention.We know that mask=[True,False,True],when axis=0,this funciton perfrom mask in each tensor(list) in first dimention,which is [1,1,1],[2,2,2] and [3,3,3],because mask's first and last element is True,it has reaturned [1,1,1] and [3,3,3].similarly,when axis=1,boolean_mask perform mask in the second dimention of tensor "a",so it returns [1,1],[2,2],[3,3],this result means that the second element in each list has been deserted.And in this function,the diffcult problem is "axis",if you can not understand the "axis" mean,you can refer to my another blogs.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章