計算張量維度上非零元素的數量。 (不推薦使用的參數)(不推薦使用的參數)
用法
tf.compat.v1.count_nonzero(
input_tensor=None, axis=None, keepdims=None, dtype=tf.dtypes.int64, name=None,
reduction_indices=None, keep_dims=None, input=None
)
參數
-
input_tensor
要減少的張量。應該是數字類型,bool
或string
。 -
axis
要減小的尺寸。如果None
(默認),減少所有維度。必須在[-rank(input_tensor), rank(input_tensor))
範圍內。 -
keepdims
如果為真,則保留長度為 1 的縮減維度。 -
dtype
輸出數據類型;默認為tf.int64
。 -
name
操作的名稱(可選)。 -
reduction_indices
軸的舊(不推薦)名稱。 -
keep_dims
keepdims
的已棄用別名。 -
input
覆蓋input_tensor。為了兼容性。
返回
- 減少的張量(非零值的數量)。
警告:不推薦使用某些參數:(keep_dims)
。它們將在未來的版本中被刪除。更新說明:keep_dims 已棄用,請改用 keepdims
警告:不推薦使用某些參數:(reduction_indices)
。它們將在未來的版本中被刪除。更新說明:reduction_indices 已棄用,請改用軸
沿 axis
中給定的尺寸減少 input_tensor
。除非 keepdims
為真,否則對於 axis
中的每個條目,張量的秩都會減少 1。如果 keepdims
為真,則保留縮減後的維度,長度為 1。
如果axis
沒有條目,則減少所有維度,並返回具有單個元素的張量。
注意:浮點與零的比較是通過精確的浮點相等檢查來完成的。小值是不是出於非零檢查的目的,四舍五入為零。
例如:
x = tf.constant([[0, 1, 0], [1, 1, 0]])
tf.math.count_nonzero(x) # 3
tf.math.count_nonzero(x, 0) # [1, 2, 0]
tf.math.count_nonzero(x, 1) # [1, 2]
tf.math.count_nonzero(x, 1, keepdims=True) # [[1], [2]]
tf.math.count_nonzero(x, [0, 1]) # 3
注意:字符串與零長度空字符串 ""
進行比較。任何大小大於零的字符串都已被視為非零。
例如:
x = tf.constant(["", "a", " ", "b", ""])
tf.math.count_nonzero(x) # 3, with "a", " ", and "b" as nonzero strings.
相關用法
- Python tf.compat.v1.confusion_matrix用法及代碼示例
- Python tf.compat.v1.convert_to_tensor用法及代碼示例
- Python tf.compat.v1.cond用法及代碼示例
- Python tf.compat.v1.constant用法及代碼示例
- Python tf.compat.v1.case用法及代碼示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代碼示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代碼示例
- Python tf.compat.v1.data.TFRecordDataset.interleave用法及代碼示例
- Python tf.compat.v1.distributions.Bernoulli.cross_entropy用法及代碼示例
- Python tf.compat.v1.Variable.eval用法及代碼示例
- Python tf.compat.v1.train.FtrlOptimizer.compute_gradients用法及代碼示例
- Python tf.compat.v1.layers.conv3d用法及代碼示例
- Python tf.compat.v1.strings.length用法及代碼示例
- Python tf.compat.v1.data.Dataset.snapshot用法及代碼示例
- Python tf.compat.v1.data.experimental.SqlDataset.reduce用法及代碼示例
- Python tf.compat.v1.feature_column.categorical_column_with_vocabulary_file用法及代碼示例
- Python tf.compat.v1.data.TextLineDataset.from_tensors用法及代碼示例
- Python tf.compat.v1.variable_scope用法及代碼示例
- Python tf.compat.v1.data.experimental.SqlDataset.as_numpy_iterator用法及代碼示例
- Python tf.compat.v1.distributions.Bernoulli.covariance用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.compat.v1.count_nonzero。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。