📜  Python – tensorflow.dynamic_partition()

📅  最后修改于: 2022-05-13 01:54:29.844000             🧑  作者: Mango

Python – tensorflow.dynamic_partition()

TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

dynamic_partition()用于将数据划分为多个分区。

示例 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])


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]
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)