zoukankan      html  css  js  c++  java
  • tf.boolean_mask

    tf.boolean_mask 的作用是 通过布尔值 过滤元素

    def boolean_mask(tensor, mask, name="boolean_mask", axis=None):
      """Apply boolean mask to tensor.

    tensor:被过滤的元素

    mask:一堆 bool 值,它的维度不一定等于 tensor

    return: mask 为 true 对应的 tensor 的元素

    当 tensor 与 mask 维度一致时,return 一维

    先看个 一维 例子

    # 1-D example
    tensor = [0, 1, 2, 3]
    mask = np.array([True, False, True, False])
    out = tf.boolean_mask(tensor, mask)
    print(sess.run(out))   # [0, 2]
    print(out.shape)        # (?,)

    再看看 mask 与 tensor 维度不同的例子

    tensor = [[1, 2], [3, 4], [5, 6]]
    mask = np.array([True, False, True])        # mask 与 tensor 维度不同
    out2 = tf.boolean_mask(tensor, mask)
    print(sess.run(out2))       # [[1, 2], [5, 6]]
    print(out2.shape)           # (?, 2)

    mask 可以用一个函数代替

    # 3-D
    tensor = tf.constant([
                    [[2,4],[4,1]],
                    [[6,8],[2,1]]],tf.float32)
    mask = tensor > 2        # 滤波器  mask 与 tensor 相同维度
    out3 = tf.boolean_mask(tensor, mask)
    print(sess.run(tensor))
    print(sess.run(mask))       # [[[False  True] [ True False]]
                                # [[ True  True] [False False]]]
    print(sess.run(out3))        # [4. 4. 6. 8.]     输出一维
    print(out3.shape)            # (?,)

    shape

    上面的 shape 是怎么回事呢?有如下规则

    假设 tensor.rank=4(m,n,p,q),则

    (1)当mask.shape=(m,n,p,q),结果返回(?,)

    (2)当mask.shape=(m,n,p),结果返回(?,q),表示 q 维度没有过滤

    (3)当mask.shape=(m,n),结果返回(?,p,q)

    (4)当mask.shape=(m),结果返回(?,n,p,q)

    参考资料:

    https://blog.csdn.net/qq_29444571/article/details/84574526

    https://www.w3cschool.cn/doc_tensorflow_python/tensorflow_python-tf-boolean_mask.html

  • 相关阅读:
    ES 6 系列
    ES 6 系列
    EChart.js 笔记二
    EChart.js 笔记一
    图像阈值_有cv2.threshold,cv2.adaptiveThreshold 等。
    几何变换——放大、镜像、平移、旋转、透视、仿射
    颜色空间转换---追踪物体
    图像算术运算——相加、相减、与、或、异或、非
    javascript中json对象与字符串互转及取值
    爬虫:Selenium + PhantomJS
  • 原文地址:https://www.cnblogs.com/yanshw/p/12376362.html
Copyright © 2011-2022 走看看