TensorFlow: convert tf.Dataset to tf.Tensor

Vivian

I want to generate windows of the range of 10:

import tensorflow as tf

dataset = tf.data.Dataset.from_tensor_slices(tf.range(10))
dataset = dataset.window(5, shift=1, drop_remainder=True)

and would like to train my model on this dataset.

To do so, those windows have to be converted to tensors. But the datatype of these windows cannot be converted via tf.convert_to_tensor to a tensor. It is possible to do tf.convert_to_tensor(list(window)) but this is quite inefficient.

Does anyone know how to convert a tf.VariantDataset efficiently to a tf.Tensor?

Thank you for your help!

jdehesa

If you want to create a tensor of sliding windows, doing it through a dataset is not really the best way, is far less efficient and flexible. I don't think there is a proper operation for that, but there are two similar ones for 2D and 3D arrays, tf.image.extract_patches and tf.extract_volume_patches. You can reshape your 1D data to use them:

import tensorflow as tf

a = tf.range(10)
win_size = 5
stride = 1
# Option 1
a_win = tf.image.extract_patches(tf.reshape(a, [1, -1, 1, 1]),
                                 sizes=[1, win_size, 1, 1],
                                 strides=[1, stride, 1, 1],
                                 rates=[1, 1, 1, 1],
                                 padding='VALID')[0, :, 0]
# Option 2
a_win = tf.extract_volume_patches(tf.reshape(a, [1, -1, 1, 1, 1]),
                                  ksizes=[1, win_size, 1, 1, 1],
                                  strides=[1, stride, 1, 1, 1],
                                  padding='VALID')[0, :, 0, 0]
# Print result
print(a_win.numpy())
# [[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]
#  [5 6 7 8 9]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is there an equivalent to tf.convert_to_tensor in tensorflow c++?

TensorFlow: How convert tf.Tensor string to python datetime datatype?

Convert a tensorflow tf.data.Dataset FlatMapDataset to TensorSliceDataset

Can't convert non-rectangular Python sequence to Tensor with tf.data.Dataset.from_tensor_slices

Cannot convert a list of "strings" to a tf.Dataset.from_tensor_slicer() - ValueError: Can't convert non-rectangular Python sequence to Tensor

TensorFlow tf.sparse_tensor_dense_matmul

ValueError: Can't convert non-rectangular Python sequence to Tensor when using tf.data.Dataset.from_tensor_slices

Converting a tf.Tensor to numpy array in tf.data.Dataset.map (graph mode) in TF 2.0

why we need tf.convert_to_tensor?

Tensorflow tf.cond encountering error: TypeError: Failed to convert object of type <class 'function'> to Tensor

tensorflow pass numpy array to graph using placeholder vs tf.convert_to_tensor()

TensorFlow 2.0: Cant run minimal TF Tutorial: TypeError: Can not convert a int64 into a Tensor or Operation

TensorFlow tf.data.Dataset and bucketing

Training using tf.Dataset in TensorFlow 2.0

Tensorflow tf.dataset.shuffle very slow

model.fit with tensorflow 2.1 and tf.data.Dataset ValueError: Attempt to convert a value TensorSpec

How to convert TF Tensor holding value into Tensor holding categorical values

tf.data.Dataset.from_tensor_slices, tensors and eager mode

Avoiding tf.data.Dataset.from_tensor_slices with estimator api

How to print the result of `tf.data.Dataset.from_tensor_slices`?

How to join two tf.data.Dataset tensor slices?

Is there a way to use tf.data.Dataset inside of another Dataset in Tensorflow?

Tensorflow tf.data.Dataset API, dataset unzip function?

What does tensorflow tf.keras.input "tensor" parameter do?

How to use tf.gather_nd to slice a tensor in tensorflow?

Extracting string with regex from a tf.Tensor in Tensorflow 2?

TensorFlow: TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed

How to change the value of a tensor which is not a tf.Variable in TensorFlow?

Tensorflow error: Using a `tf.Tensor` as a Python `bool` is not allowed