1D/2D Tensor strided sliding window
Hi @niemiaszek  ,

I have replicated the reported behaviour with colab using TF v2.14, 2.15 and tf-nightly. Please find the [gist ](https://colab.research.google.com/gist/Venkat6871/534a54b6bfe499c8bd7b41fb148a02c6/62526_2-14_2-15-nightly.ipynb)here for reference.

Thank you! > Hi @moberweger ,
> 
> I have replicated the reported behaviour with colab using TF v2.14, 2.15 and tf-nightly. Please find the [gist ](https://colab.research.google.com/gist/Venkat6871/534a54b6bfe499c8bd7b41fb148a02c6/62526_2-14_2-15-nightly.ipynb)here for reference.
> 
> Thank you!

@Venkat6871 I guess you pinged the wrong person under this issue, as this person created different issue.
Anyway, I've checked your gist and it's accurate, replicating my behaviour. Thanks. Hello @Venkat6871 , @SuryanarayanaY . Is it possible to get some tensorflower assigned to help with this issue?

I've managed to resolve this issue by using `tf.image.extract_patches` as slicing mechanism for 1D or 2D arrays, but honestly this is not an universal solution and I can imagine getting into trouble in future where there is no such native high-level abstract op.

I know that it's probably my skill issue in writing tf.function code for usage in tf.data pipelines, but I couldn't really find any general solution Hi **@niemiaszek** ,

Sorry for the delay, I will share another approach with you now is to use tf.signal.frame, which is designed for creating frames of a signal (which can be your audio data or melspectrogram data) with specified frame length and step. This function will be suitable for creating a strided sliding window.

Here you are encountering error is due to trying to use python's native iteration and slicing mechanisms directly on tensorflow tensors within a graph-executed context such as within @tf.function or when using tf.data.Dataset.map.

TensorFlow's execution model doesn't allow for Python-level iteration over tensors directly because the shapes and operations need to be graph-compile-time determinable for performance and distributability.

Here's how you can adjust your function to use tf.signal.frame for creating a sliding window over a 1D tensor (like your audio data). If your tensor has more dimensions (like your MelSpectrogram [time, freq]), you may need to adjust the approach slightly, but the principle remains the same:

Here I am providing [gist](https://colab.sandbox.google.com/gist/Venkat6871/6e9d8b31f110c495c7c5ffa1abd3955a/62526_2-15-nightly-v.ipynb) for your reference.

This code uses tf.signal.frame to create the sliding window over the tensor. Note that tf.signal.frame expects at least a 2D tensor, so if you're working with a 1D tensor (like an audio waveform), you might need to expand its dimensions first. After processing, you can optionally squeeze the tensor to remove any unwanted dimensions.

Thank you! This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/62526">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/62526">No</a>

### Issue type

Support

### Have you reproduced the bug with TensorFlow Nightly?

No

### Source

binary

### TensorFlow version

2.13

### Custom code

Yes

### OS platform and distribution

ubuntu 22.04

### Mobile device

_No response_

### Python version

3.10

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

I tried to implement strided sliding window over Audio `[None,]` or MelSpectrogram `[time, freq]`. It seems like very straight-forward thing to make over regular python array.

I had some implementation in notebook which worked in eager mode and with @tf.function decorator:
```python
@tf.function
def slice_tensor(tensor): 
    s_size = 3
    s_stride = 2
    slices = tf.stack([input[i : i + s_size , ...] for i in range(0, tensor.shape[0]-s_size+1, s_stride)])

    return slices

slice_tensor(tf.range(9))
```
```
<tf.Tensor: shape=(4, 3), dtype=int32, numpy=
array([[0, 1, 2],
       [2, 3, 4],
       [4, 5, 6],
       [6, 7, 8]], dtype=int32)>
```

However, using it in tf.data pipeline resulted first in:
```python
tf.data.Dataset.range(8,12).map(lambda x: tf.range(x)).map(slice_tensor)
```
`TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'`.

I switched from `tensor.shape[0]` to `tf.shape(tensor)[0]`, which results in:
```OperatorNotAllowedInGraphError: Iterating over a symbolic `tf.Tensor` is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.```

I was struggling to come up with some solution to this and ended using  `tf.image.extract_patches()`, but it makes my data pipeline more convoluted and seems like an overkill for my task. I tried looking up for `extract_patches()` implementation for some inspiration but it's not python op.
Does anyone know how to deal with such problem? I can imagine it requires using some tensorflow-native mapping or looping mechanisms like `tf.map_fn()`.

### Standalone code to reproduce the issue

```shell
@tf.function
def slice_tensor(tensor): 
    s_size = 3
    s_stride = 2
    slices = tf.stack([input[i : i + s_size , ...] for i in range(0, tf.shape(tensor)[0]-s_size+1, s_stride)])
    return slices

tf.data.Dataset.range(8,12).map(lambda x: tf.range(x)).map(slice_tensor)
```


### Relevant log output

```shell
OperatorNotAllowedInGraphError: in user code:

    File "/tmp/ipykernel_14140/728857128.py", line 5, in slice_tensor  *
        slices = tf.stack([input[i : i + s_size , ...] for i in range(0, tf.shape(tensor)[0]-s_size+1, s_stride)])

    OperatorNotAllowedInGraphError: Iterating over a symbolic `tf.Tensor` is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.
```
