Python - tensorflow.dynamic_partition()
Last Updated :
10 Jul, 2020
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)
Similar Reads
Python - tensorflow.dynamic_stitch() TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. dynamic_stitch() is used to merge multiple tensors into single tensor. Syntax: tensorflow.dynamic_stitch( indices, data, name) Parameter: indices: It is a list of Tensor
2 min read
Python - tensorflow.identity_n() TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. identity_n() is used get a list of Tensor with same shape and content as input Tensor. Syntax: tensorflow.identity_n( input, name) Parameters: input:  It is a Tensor.na
2 min read
Python - tensorflow.expand_dims() TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. expand_dims() is used to insert an addition dimension in input Tensor. Syntax: tensorflow.expand_dims( input, axis, name) Parameters: input: It is the input Tensor.axis
2 min read
Python - tensorflow.fingerprint() TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. fingerprint() is used to generate fingerprint value. Syntax: tensorflow.fingerprint( data, method, name) Parameters: data: It is a Tensor having rank 1 or higher.method
2 min read
Tensorflow - linspace() in Python TensorFlow is an open-source Python library designed by Google to develop Machine Learning models and deep learning neural networks. While working with TensorFlow many times we need to generate evenly-spaced values in an interval. tensorflow.linspace(): This method takes starting tensor, ending tens
2 min read
Python - tensorflow.gather_nd() TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. gather_nd() is used to gather the slice from input tensor based on the indices provided. Syntax: tensorflow.gather_nd( params, indices, batch_dims, name) Parameters: pa
2 min read