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


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

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

segment_max()用於在張量的分段中找到最大元素。

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

參數:

  • data:它是張量。允許的dtypes是float32,float64,int32,uint8,int16,int8,int64,bfloat16,uint16,half,uint32,uint64。
  • segment_ids:它是帶有排序值的一維張量。它的大小應等於數據一維的大小。允許的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]) 
segment_ids = tf.constant([2, 2, 2]) 
  
# Printing the input tensor 
print('data:', data) 
print('segment_ids:', segment_ids) 
  
# Calculating result 
res = tf.math.segment_max(data, segment_ids) 
  
# Printing the result 
print('Result:', res)

輸出:

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


範例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]]) 
segment_ids = tf.constant([0, 0, 2]) 
  
# Printing the input tensor 
print('data:', data) 
print('segment_ids:', segment_ids) 
  
# Calculating result 
res = tf.math.segment_max(data, segment_ids) 
  
# Printing the result 
print('Result:', res)

輸出:

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



相關用法


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