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


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

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

dynamic_partition()用於將數據劃分為多個分區。

用法:tensorflow.dynamic_partition(data, partitions, num_partitions, name)

參數:

  • data:需要對輸入張量進行分區。
  • partitions:它是int32類型的Tensor,其數據應在[0,num_partitions)範圍內。
  • num_partitions:它定義了分區的數量。
  • name(optional):它定義了操作的名稱。

返回值:



它返回帶有num_partitions個項目的張量列表。列表中的每個張量都具有與數據相同的dtype。

範例1:將數據分為兩個分區

Python3

# Importing the library 
import tensorflow as tf 
  
# Initializing the input 
data = [1, 2, 3, 4, 5] 
num_partitions = 2
partitions = [0, 0, 1, 0, 1] 
  
# Printing the input 
print('data:', data) 
print('partitions:', partitions) 
print('num_partitions:', num_partitions) 
  
# Calculating result 
x = tf.dynamic_partition(data, partitions, num_partitions) 
  
  
# Printing the result 
print('x[0]:', x[0]) 
print('x[1]:', x[1])

輸出:

data: [1, 2, 3, 4, 5]
partitions:[0, 0, 1, 0, 1]
num_partitions:2
x[0]: tf.Tensor([1 2 4], shape=(3, ), dtype=int32)
x[1]: tf.Tensor([3 5], shape=(2, ), dtype=int32)


範例2:分為3個張量

Python3

# Importing the library 
import tensorflow as tf 
  
# Initializing the input 
data = [1, 2, 3, 4, 5, 6, 7] 
num_partitions = 3
partitions = [0, 2, 1, 0, 1, 2, 2] 
  
# Printing the input 
print('data:', data) 
print('partitions:', partitions) 
print('num_partitions:', num_partitions) 
  
# Calculating result 
x = tf.dynamic_partition(data, partitions, num_partitions) 
  
  
# Printing the result 
print('x[0]:', x[0]) 
print('x[1]:', x[1]) 
print('x[2]:', x[2])

輸出:

data: [1, 2, 3, 4, 5, 6, 7]
partitions:[0, 2, 1, 0, 1, 2, 2]
num_partitions:3
x[0]: tf.Tensor([1 4], shape=(2, ), dtype=int32)
x[1]: tf.Tensor([3 5], shape=(2, ), dtype=int32)
x[2]: tf.Tensor([2 6 7], shape=(3, ), dtype=int32)



相關用法


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