codecamp

TensorFlow 在张量上应用布尔掩码

tf.boolean_mask

boolean_mask ( 
    tensor , 
    mask , 
    name = 'boolean_mask' 
)

定义在:tensorflow/python/ops/array_ops.py.

参见指南:张量变换>分割和连接

对张量应用布尔掩码.Numpy 等同于 tensor[mask].

#1-D 示例 
tensor =  [ 0 , 1 , 2 , 3 ] 
mask = np.array([True,False,True,False] ) 
boolean_mask (tensor,mask) == >  [ 0 , 2 ]

一般情况下,0 < dim(mask) = K <= dim(tensor) 并且 mask 的形状必须与张量形状的第一 K 维度匹配.然后我们就有:(tensor, mask)[i, j1,...,jd] = tensor[i1,...,iK,j1,...,jd] 当(i1,...,iK) 是 mask (row-major order) 的第 i 个真输入.

ARGS:

  • tensor:N -D 张量.
  • mask:K - D 布尔张量,K <= N 和 K 必须是静态已知的.
  • name:此操作的名称(可选).

返回:

返回(N-K + 1)维张量由条目中填充的 tensor 对应于 mask 中的 True 值.

注意:

  • ValueError:如果形状不符合时.

例子:

#2-D示例 
tensor =  [ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] ] 
mask = np.array([True,False,True] ) 
boolean_mask (tensor,mask) == >  [ [ 1 , 2 ] , [ 5 , 6 ] ]


TensorFlow 张量类型转换
TensorFlow 返回广播形状
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

TensorFlow 函数介绍

TensorFlow 函数模块:tf

TensorFlow的image模块

TensorFlow使用之tf.io

TensorFlow使用之tf.keras

TensorFlow函数教程:tf.keras.applications

TensorFlow函数教程:tf.keras.backend

TensorFlow使用之tf.metrics

TensorFlow使用之tf.nn

TensorFlow使用之tf.python_io

TensorFlow 功能函数

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }