Thursday, October 2, 2025
HomeLanguagesTensorflow | tf.data.Dataset.from_tensor_slices()

Tensorflow | tf.data.Dataset.from_tensor_slices()

With the help of tf.data.Dataset.from_tensor_slices() method, we can get the slices of an array in the form of objects by using tf.data.Dataset.from_tensor_slices() method.

Syntax : tf.data.Dataset.from_tensor_slices(list)
Return : Return the objects of sliced elements.

Example #1 :
In this example we can see that by using tf.data.Dataset.from_tensor_slices() method, we are able to get the slices of list or array.




# import tensorflow
import tensorflow as tf
  
# using tf.data.Dataset.from_tensor_slices() method
gfg = tf.data.Dataset.from_tensor_slices([1, 2, 3, 4, 5])
  
for ele in gfg:
    print(ele.numpy())


Output :

1
2
3
4
5

Example #2 :




# import tensorflow
import tensorflow as tf
  
# using tf.data.Dataset.from_tensor_slices() method
gfg = tf.data.Dataset.from_tensor_slices([[5, 10], [3, 6]])
  
for ele in gfg:
    print(ele.numpy())


Output :

[5, 10]
[3, 6]

RELATED ARTICLES

Most Popular

Dominic
32331 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11927 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7080 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS