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

tf.boolean_mask的更多相关文章

  1. tensorflow 基本函数(1.tf.split, 2.tf.concat,3.tf.squeeze, 4.tf.less_equal, 5.tf.where, 6.tf.gather, 7.tf.cast, 8.tf.expand_dims, 9.tf.argmax, 10.tf.reshape, 11.tf.stack, 12tf.less, 13.tf.boolean_mask

    1.  tf.split(3, group, input)  # 拆分函数    3 表示的是在第三个维度上, group表示拆分的次数, input 表示输入的值 import tensorflow ...

  2. tensorflow中的boolean_mask

    将mask中所有为true的抽取出来,放到一起,这里从n维降到1维度 tensor = [[1, 2], [3, 4], [5, 6]] import numpy as np mask=np.arra ...

  3. TF随笔-10

    #!/usr/bin/env python# -*- coding: utf-8 -*-import tensorflow as tf x = tf.constant(2)y = tf.constan ...

  4. 解释张量及TF的一些API

    张量的定义 张量(Tensor)理论是数学的一个分支学科,在力学中有重要应用.张量这一术语起源于力学,它最初是用来表示弹性介质中各点应力状态的,后来张量理论发展成为力学和物理学的一个有力的数学工具.张 ...

  5. 第七节,TensorFlow编程基础案例-TensorBoard以及常用函数、共享变量、图操作(下)

    这一节主要来介绍TesorFlow的可视化工具TensorBoard,以及TensorFlow基础类型定义.函数操作,后面又介绍到了共享变量和图操作. 一 TesnorBoard可视化操作 Tenso ...

  6. DeepLearning.ai-Week3-Autonomous driving-Car detection

    1 - Import Packages import argparse import os import matplotlib.pyplot as plt from matplotlib.pyplot ...

  7. 课程四(Convolutional Neural Networks),第三 周(Object detection) —— 2.Programming assignments:Car detection with YOLOv2

    Autonomous driving - Car detection Welcome to your week 3 programming assignment. You will learn abo ...

  8. Autonomous driving - Car detection YOLO

    Andrew Ng deeplearning courese-4:Convolutional Neural Network Convolutional Neural Networks: Step by ...

  9. 『TensorFlow』SSD源码学习_其五:TFR数据读取&数据预处理

    Fork版本项目地址:SSD 一.TFR数据读取 创建slim.dataset.Dataset对象 在train_ssd_network.py获取数据操作如下,首先需要slim.dataset.Dat ...

随机推荐

  1. 11 —— 回顾 JSON 相互转换的知识点

    /** * json 转换的两种方式 * * 一,转为字符串 (序列化的过程) * JSON.stringify() * * 二,json 转化为字符串 (反序列化的过程) * JSON.parse( ...

  2. RN命令的使用

    RN中文网站 https://reactnative.cn/docs/getting-started/ 创建项目 1.最新版本项目react-native init MyApp 使用可行版本  rea ...

  3. 卷积神经网络应用于tensorflow手写数字识别(第三版)

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...

  4. 题解P4201: [NOI2008]设计路线

    发现给出了一棵树, 不是树的情况直接输出-1 考虑进行DP, 设f[i][0/1/2]为i的子树中选小于等于0/1/2条边修路的方案数, 不妨对于一个节点, 先考虑正好相等的情况, 假设当前扫到了一个 ...

  5. xlua 原理

    基于版本 104 可以直接在lua访问c#函数原理: CS 是一个table,设置了一个__index函数,如果访问不存在的成员的时候,会走__index函数,调用import_type从C#中找到具 ...

  6. Exchange 2016 OWA更改css样式

    css文件目录:E:\Exchange 2016\FrontEnd\HttpProxy\owa\auth\15.1.1713\themes\resources\logon.css ##更改左侧页面颜色 ...

  7. JKS not Found

    近期使用Spring Boot开发微信验证的时候, 在获取token时,Idea老是提示Jks not found,网上找资料,都说是SSL的问题 实际解决方法: 重装JDK,将JDK重装之后,运行正 ...

  8. 题解 P4781 【【模板】拉格朗日插值】

    题目 本蒟蒻看到一道数学题,就顺手切了.感觉单单对这一题而言,部分评论区的大佬过于复杂了 [分析] 先讲讲拉格朗日插值法: 对于给定的 \((n+1)\) 个点,我们可以确定唯一的一个 至多\(n\) ...

  9. 如何写出高质量的Python代码--做好优化--改进算法点滴做起

    小伙伴你的程序还是停留在糊墙吗?优化代码可以显示程序员的素质欧! 普及一下基础了欧: 一层for简写:y = [1,2,3,4,5,6],[(i*2) for i in y ]       会输出  ...

  10. 吴裕雄--天生自然Django框架开发笔记:Django简介

    Python下有许多款不同的 Web 框架.Django是重量级选手中最有代表性的一位.许多成功的网站和APP都基于Django. Django是一个开放源代码的Web应用框架,由Python写成. ...