How to create custom loss function with fft in Keras

Viewed 940

I would like to compute a loss function but I have a problem in using the fft function. Here the code with numpy:

def bidule(y_true, y_pred):
    product = np.fft.fft(y_true) * np.fft.fft( y_pred).conj()
    cc = np.fft.fftshift(np.fft.ifft(product))
    return np.argmax(cc.real)

I would like to use the Keras backends but when I replace np by K the code don't run. "module 'keras.backend' has no attribute 'fft'"

1 Answers

Please first check the Keras Backend Documentation to see if there is a .fft attribute. There is no .fft in the keras.backend. However, you can implement one with tf.signal.fft using TensorFlow by using a Lambda layer with Keras. One example of using Lambda with a spectral fourier function in the following link. You can implement yours with the given link.

Related