TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning neural networks.
dynamic_partition() is used to divide the data into number of partitions.
Syntax: tensorflow.dynamic_partition(data, partitions, num_partitions, name)
Parameters:
- data : It is the input tensor that need to be partitioned.
- partitions: It is Tensor of type int32 and it’s data should be in the range [0, num_partitions).
- num_partitions: It defines the number of partitions.
- name(optional): It defines the name for the operation.
Returns:
It returns a list of tensor with num_partitions items. Each tensor in the list have same dtype as data.
Example 1: Dividing data into two partitions
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 ]) |
Output:
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)
Example 2: Dividing into 3 Tensors
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 ]) |
Output:
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)