Understand the Difference Between numpy.split() and tf.split(): A Beginner Guide – TensorFlow Tutorial

By | December 6, 2019

Both numpy.split() and tf.split() can split an array or a tensor into some sub arrays and tensors. However, there exsits some differences between them. In this tutorial, we will compare with them to help you understand.

Syntax of numpy.split() and tf.split

numpy.split(ary, indices_or_sections, axis=0) tf.split(value, num_or_size_splits, axis=0, num=None, ame=’split’ )

From syntax of numpy.split() and tf.split(), we will find the main difference between them is on the second parameter.

As to numpy.split()

indices_or_sections: The value of it determines the way to get sub arrays from ary is to use indicues.

For example, as to code below

x = np.array([1, 2, 3, 4, 2, 3, 4, 5], dtype = np.float32)

xs = np.split(x,indices_or_sections = [1, 2, 3], axis = 0)

indices_or_sections = [1, 2, 3], which means we will get sub arrays from x by indices, these sub arrays are: x[0: 1], x[1: 2], x[2: 3] and x[3:]

You can get more from tutorial below.

Understand numpy.split(): Split an Array into Sub-Arrays – NumPy Tutorial

As to tf.split()

num_or_size_splits: The value of it determines the element counts of sub tensors.

As to code below

x = tf.Variable(np.array([1, 2, 3, 4, 2, 3, 4, 5], dtype = np.float32))

xs = tf.split(x,num_or_size_splits = [1, 2, 5], axis = 0)

Which means we will split tensor x into 3 tensors, the first tensor contains 1 elements, the second tensor contains 2 elements and the last tensor contains 5 elements.

You can find more details in this tutorial:

TensorFlow tf.split(): Splits a Tensor into Sub Tensors – TensorFlow Tutorial

Understand the difference between numpy.split() and tf.split(), we can use them correctly.

Leave a Reply