Tavakoli
Tavakoli

Reputation: 1375

How can slicing dataset in TensorFlow?

I want slicing dataset in tf.data. My data is like this:

dataset = tf.data.Dataset.from_tensor_slices([[0, 1, 2, 3, 4],
                                              [1, 2, 3, 4, 5],
                                              [2, 3, 4, 5, 6],
                                              [3, 4, 5, 6, 7],
                                              [4, 5, 6, 7, 8]])

Then the main data is:

[0 1 2 3 4]
[1 2 3 4 5]
[2 3 4 5 6]
[3 4 5 6 7]
[4 5 6 7 8]

I want create other tensor dataset that contain data like this:

       [[1, 2],
       [2, 3],
       [3, 4],
       [4, 5],
       [5, 6]]

In the numpy it is like this:

dataset[:,1:3]

How can do this in TensorFlow?

Update:

I do that with this:

dataset2 = dataset.map(lambda data: data[1:3])
for val in dataset2:
    print(val.numpy())

But I think there is good solutions.

Upvotes: 3

Views: 3071

Answers (1)

user11530462
user11530462

Reputation:

According to me your solution is a best solution. For the benefit of community, i am using as_numpy_iterator() method of tf.data.Dataset to slice dataset (small syntax change to your code).

Please refer code below

import tensorflow as tf

dataset = tf.data.Dataset.from_tensor_slices([[0, 1, 2, 3, 4],
                                              [1, 2, 3, 4, 5],
                                              [2, 3, 4, 5, 6],
                                              [3, 4, 5, 6, 7],
                                              [4, 5, 6, 7, 8]])


dataset2 = dataset.map(lambda data: data[1:3])
for val in dataset2.as_numpy_iterator():
    print(val)

Output:

[1 2]
[2 3]
[3 4]
[4 5]
[5 6]

Upvotes: 1

Related Questions