codecamp

计算TensorFlow中非零元素的个数

tf.count_nonzero

count_nonzero(
    input_tensor,
    axis=None,
    keep_dims=False,
    dtype=tf.int64,
    name=None,
    reduction_indices=None
)

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

参考指南:数学函数>减少张量的计算

在张量的维度上计算的非零元素的个数.

沿轴线给定的维度减少 input_tensor.除非 keep_dims 是 true,否则张量的秩将在轴的每个条目中减少1.如果 keep_dims 为 true,则减小的维度将保持长度为1.
如果轴没有条目,则会减少所有维度,并返回具有单个元素的张量.
注意,浮点与零的比较是通过精确的浮点相等性检查完成的.对于非零检查,小值不四舍五入为零.

例如:

# 'x' is [[0, 1, 0]
#         [1, 1, 0]]
tf.count_nonzero(x) ==> 3
tf.count_nonzero(x, 0) ==> [1, 2, 0]
tf.count_nonzero(x, 1) ==> [1, 2]
tf.count_nonzero(x, 1, keep_dims=True) ==> [[1], [2]]
tf.count_nonzero(x, [0, 1]) ==> 3

ARGS:

  • input_tensor:要减少的张量.应该是数字类型,或 bool.
  • axis:要减少的维度.如果为 None(默认值),则减少所有维度.
  • keep_dims:如果为 true,则保留长度为1的缩小维度.
  • dtype:输出 dtype,默认为 tf.int64.
  • name:操作的名称(可选).
  • reduction_indices:轴的旧名称(不推荐).

返回:

减少的张量(非零值的数量).

TensorFlow元素的双曲余弦
TensorFlow对ref进行递增
温馨提示
下载编程狮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; }