TensorFlow是Google設計的開源Python庫,用於開發機器學習模型和深度學習神經網絡。 TensorFlow的數學模塊中包含bincount()。它用於計算整數數組中每個數字的出現次數。
用法:tensorflow.math.bincount( arr, weights, minlength, maxlength, dtype, name)
參數:
- arr:它是具有非負值的dtype int32的張量。
- weights(optional):它是與arr形狀相同的張量。 arr中每個值的計數都會增加其相應的權重。
- minlength(optional):它定義了返回輸出的最小長度。
- maxlength(optional):它定義了返回輸出的最大長度。不會計算arr中大於或等於maxlength的值的bin。
- dtype(optional):如果weight為none,它將確定返回輸出的dtype。
- name(optional):這是一個可選參數,用於定義操作的名稱。
返回值:
它返回一個與權重具有相同dtype或給定dtype的向量。向量的索引定義了值,值定義了arr中的索引bin。
範例1:
Python3
# importing the library
import tensorflow as tf
# initializing the input
a = tf.constant([1,2,3,4,5,1,7,3,1,1,5], dtype = tf.int32)
# printing the input
print('a:',a)
# evaluating bin
r = tf.math.bincount(a)
# printing result
print("Result:",r)
輸出:
a: tf.Tensor([1 2 3 4 5 1 7 3 1 1 5], shape=(11,), dtype=int32) Result: tf.Tensor([0 4 1 2 1 2 0 1], shape=(8,), dtype=int32) # bin of 0 in input is 0, bin of 1 in input is 4 and so on
範例2:本示例提供權重,因此將值乘以相應的權重而不是1。
Python3
# importing the library
import tensorflow as tf
# initializing the input
a = tf.constant([1,2,3,4,5,1,7,3,1,1,5], dtype = tf.int32)
weight = tf.constant([0,2,1,0,2,1,3,3,1,0,5], dtype = tf.int32)
# printing the input
print('a:',a)
print('weight:',weight)
# evaluating bin
r = tf.math.bincount(arr = a,weights = weight)
# printing result
print("Result:",r)
輸出:
a: tf.Tensor([1 2 3 4 5 1 7 3 1 1 5], shape=(11,), dtype=int32) weight: tf.Tensor([0 2 1 0 2 1 3 3 1 0 5], shape=(11,), dtype=int32) Result: tf.Tensor([0 2 2 4 0 7 0 3], shape=(8,), dtype=int32)
相關用法
注:本文由純淨天空篩選整理自aman neekhara大神的英文原創作品 Python – tensorflow.math.bincount()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。