當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tensorflow.math.unsorted_segment_sum()用法及代碼示例

TensorFlow是Google設計的開源Python庫,用於開發機器學習模型和深度學習神經網絡。

unsorted_segment_sum()用於查找段的總和。

用法: tensorflow.math.unsorted_segment_sum( data, segment_ids, num_segments, name )

參數:

  • data:它是張量。允許的dtype是浮點數或複數。
  • segment_ids:它是帶有排序值的一維張量。它的大小應等於數據一維的大小。它代表不同的段ID的數量。允許的dtype是int32和int64。
  • num_segments:它是張量。允許的dtype是int32和int64。
  • name(optional):它定義了操作的名稱。

返回:它返回dtype的張量作為x。



範例1:

Python3

# importing the library 
import tensorflow as tf 
  
# Initializing the input tensor 
data = tf.constant([1, 2, 3], dtype = tf.float64) 
segment_ids = tf.constant([2, 2, 2]) 
  
# Printing the input tensor 
print('data:', data) 
print('segment_ids:', segment_ids) 
  
# Calculating result 
res = tf.math.unsorted_segment_sum(data, segment_ids, tf.constant(3)) 
  
# Printing the result 
print('Result:', res)

輸出:

data: tf.Tensor([1. 2. 3.], shape=(3, ), dtype=float64)
segment_ids: tf.Tensor([2 2 2], shape=(3, ), dtype=int32)
Result: tf.Tensor([0. 0. 6.], shape=(3, ), dtype=float64)




範例2:

Python3

# importing the library 
import tensorflow as tf 
  
# Initializing the input tensor 
data = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype = tf.float64) 
segment_ids = tf.constant([0, 0, 2]) 
  
# Printing the input tensor 
print('data:', data) 
print('segment_ids:', segment_ids) 
  
# Calculating result 
res = tf.math.unsorted_segment_sum(data, segment_ids, tf.constant(3)) 
  
# Printing the result 
print('Result:', res)

輸出:

data: tf.Tensor(
[[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]], shape=(3, 3), dtype=float64)
segment_ids: tf.Tensor([0 0 2], shape=(3, ), dtype=int32)
Result: tf.Tensor(
[[5. 7. 9.]
 [0. 0. 0.]
 [7. 8. 9.]], shape=(3, 3), dtype=float64)



相關用法


注:本文由純淨天空篩選整理自aman neekhara大神的英文原創作品 Python – tensorflow.math.unsorted_segment_sum()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。