How can I achive the hilbert transform by using tensorflow?
```
from scipy.signal import hilbert
import tensorflow as tf
import numpy as np

def is_complex(x):
    return x.dtype == tf.complex64 or x.dtype == tf.complex128

def highpass_filter(x, N=None, axis=-1):
    x = tf.convert_to_tensor(x)
    if is_complex(x):
        raise ValueError("x must be real.")
    if N is None:
        N = x.shape[axis]
    if N <= 0:
        raise ValueError("N must be positive.")
    
    from tensorflow.signal import fft
    Xf = fft(x, N, axis=axis)
    h = tf.zeros(N)
    if N % 2 == 0:
        h = tf.tensor_scatter_nd_update(h, [[0], [N // 2]], [1, 1])
        h = tf.tensor_scatter_nd_update(h, tf.range(1, N // 2), tf.ones(N // 2 - 1) * 2)
    else:
        h = tf.tensor_scatter_nd_update(h, [0], [1])
        h = tf.tensor_scatter_nd_update(h, tf.range(1, (N + 1) // 2), tf.ones((N + 1) // 2 - 1) * 2)

    if x.ndim > 1:
        ind = [tf.newaxis] * x.ndim
        ind[axis] = slice(None)
        h = h[tuple(ind)]

    x = tf.signal.ifft(Xf * h, axis=axis)
    return x

x = np.random.rand(1, 4096, 2)
x_h = hilbert(np.squeeze(highpass_filter(x,axis=1)), axis=1)
```

Regarding your question about converting a **Tensor/KerasTensor** to a **NumPy** array, you can use the **numpy()** method of the tensor to convert it to a NumPy array. For example, if x is a **Tensor/KerasTensor**, you can convert it to a NumPy array as follows:
```
x_np = x.numpy()
```

To implement the Hilbert transform on a **Tensor/KerasTensor**, you can use the hilbert function from **scipy.signal**
```
# generate a test signal
x = np.random.rand(1, 4096, 2)

# apply the Hilbert transform
x_hilbert = hilbert(x, axis=1)
```
To convert a **Tensor/KerasTensor** to a NumPy array, you can use the **numpy()** method of the tensor.
```
# generate a test tensor
x = tf.constant(np.random.rand(1, 4096, 2))

# convert the tensor to a NumPy array
x_np = x.numpy()
``` sorry, i tried the following methods to convert the data[Tensor/KerasTensor] to data[Numpy]
1. data_np = data.numpy()
2. data_np = keras.backend.get_val(data)
but neither works. It shows: AttributeError: 'Tensor' object has no attribute 'numpy'. So I can't use scipy.signal.hilbert to implement a Hilbert transform on data[Tensor/KerasTensor]. This may be due to the fact that my data is generated from:
def Net():
      input = keras.layers.Input(shape=(64, 64, 3), dtype="float32")
      conv1 = keras.layers.Conv2D(filters=3, kernel_size=3, padding="same")
      ……
      conv2 = keras.layers.Conv2D(filters=2, kernel_size=3, padding="same")   #shape(1,64,64,2)
      data = reshape(conv2, [1, 4096, 2]) Hi @Evan-0715, 
I tried replicating the issue, but I'm facing a different error. Please find the gist [here](https://colab.sandbox.google.com/gist/synandi/37cd6070a8f365994c9339af6593be98/60119.ipynb). 
Kindly refer the error below.
```
AttributeError: 'DType' object has no attribute 'kind'
```
Thank you! I think this may be caused by the randomly generated numpy array dtype being in the wrong format, try replacing x = np.random.rand(1, 4096, 2) with x = [[[111,225],[225,445],[333,225],[445,445]]], it's just an artificial numpy array after all. I hope this helps. If you can run this code without any problems, you should get the following error: 
TypeError: fft() got an unexpected keyword argument 'axis'.
So, using tf.signal.fft or tf.signal.ifft does not achieve the same effect as scipy.signal.hilbert. So it is probably better to convert data[tensor] to data[numpy]. I hope you can help me realise this idea, thanks! Hi @Evan-0715, I was able to replicate the error in Colab, kindly find the gist [here](https://colab.sandbox.google.com/gist/synandi/897b8bfbf0c887b9920072edc36ddeb7/60119_1.ipynb). To overcome the error, please use `numpy.tff.tff()` instead of `tf.signal.tff` in order to use the `axis` argument. Kindly find the gist [here](https://colab.sandbox.google.com/gist/synandi/91a7d7047f7e5f6a969b6fd282f6c953/60119_2.ipynb) after using `numpy.tff.tff()`. The code you have shared is raising another error like below. 
```
InvalidArgumentError: {{function_node __wrapped__TensorScatterUpdate_device_/job:localhost/replica:0/task:0/device:CPU:0}} Inner dimensions of output shape must match inner dimensions of updates shape. Output: [4096] updates: [2047] [Op:TensorScatterUpdate]
```

Thank you! This new error may be related to my own calculation process. And I reworked the tensorflow code for the Hilbert transform and now it runs smoothly on my computer. The code is as follows:
```
import tensorflow as tf
import numpy as np

def highpass_filter(x, N=None, axis=-1):
    x = tf.convert_to_tensor(x)
    if x.dtype == tf.complex64 or x.dtype == tf.complex128:
        raise ValueError("x must be real.")
    if N is None:
        N = x.shape[axis]
    if N <= 0:
        raise ValueError("N must be positive.")
    Xf = np.fft.fft(x, N, axis=axis)
    h = np.zeros(N)
    if N % 2 == 0:
        h[0] = h[N // 2] = 1
        h[1:N // 2] = 2
    else:
        h[0] = 1
        h[1:(N + 1) // 2] = 2
    if x.ndim > 1:
        ind = [tf.newaxis] * x.ndim
        ind[axis] = slice(None)
        h = h[tuple(ind)]
    x = np.fft.ifft(Xf * h, axis=axis)
    x = tf.convert_to_tensor(x)
    return x
x = np.random.rand(1, 4096, 2)
y = highpass_filter(x, `axis=1)
```
 All in all, thanks a lot! It will help me a lot in my experiments! 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/60119">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/60119">No</a>

<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Support

### Have you reproduced the bug with TF nightly?

Yes

### Source

source

### Tensorflow Version

tf 2.8

### Custom Code

Yes

### OS Platform and Distribution

Windows 10

### Mobile device

_No response_

### Python version

3.9

### Bazel version

_No response_

### GCC/Compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current Behaviour?

```shell
When I discovered that tensorflow did not have a built-in API for implementing the Hilbert transform, I tried to pass the tensor to the scipy.signal.hilbert method in numpy, but keras does not allow the tensor to be processed directly using numpy maths methods, so I used the following method for data[ Tensor/KerasTensor(1,4096,2)] to convert:
1. data_np = data.numpy()
2. data_np = keras.backend.get_val(data)
Neither works. I then decided to build my own hilbert_transform function by referring to the scipy.signal.hilbert method in numpy, at which point another problem arose:
The tf.signal.fft method is inconsistent with the scipy.fft method. scipy.fft can take an axis argument, but tf.signal.fftz cannot.
Therefore, I would like your help in answering:
1. how do I implement the Hilbert transform on data[Tensor/KerasTensor(1,4096,2)]?
or
2. how do I convert data[Tensor/KerasTensor(1,4096,2)] into a numpys array?

Translated with www.DeepL.com/Translator (free version)
```


### Standalone code to reproduce the issue

```shell
from scipy.signal import hilbert
import tensorflow as tf
import numpy as np

def is_complex(x):
    return x.dtype == tf.complex64 or x.dtype == tf.complex128
def highpass_filter(x, N=None, axis=-1):
    x = tf.convert_to_tensor(x)
    if is_complex(x):
        raise ValueError("x must be real.")
    if N is None:
        N = x.shape[axis]
    if N <= 0:
        raise ValueError("N must be positive.")

    Xf = fft(x, N, axis=axis)
    h = tf.zeros(N)
    if N % 2 == 0:
        h = tf.tensor_scatter_nd_update(h, [[0], [N // 2]], [1, 1])
        h = tf.tensor_scatter_nd_update(h, tf.range(1, N // 2), tf.ones(N // 2 - 1) * 2)
    else:
        h = tf.tensor_scatter_nd_update(h, [0], [1])
        h = tf.tensor_scatter_nd_update(h, tf.range(1, (N + 1) // 2), tf.ones((N + 1) // 2 - 1) * 2)

    if x.ndim > 1:
        ind = [tf.newaxis] * x.ndim
        ind[axis] = slice(None)
        h = h[tuple(ind)]

    x = tensorflow.signal.ifft(Xf * h, axis=axis)
    return x
x = np.random.rand(1, 4096, 2)
x_h = highpass_filter(x,axis=1)
```


### Relevant log output

_No response_</details>