Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@0xfe
Last active March 2, 2020 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xfe/5a029d815b9a5f22985933cee1a71562 to your computer and use it in GitHub Desktop.
Save 0xfe/5a029d815b9a5f22985933cee1a71562 to your computer and use it in GitHub Desktop.
Neural Network to learn the Discrete Fourier Transform
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
batch_size = 15000
fft_size = 128
xs = np.random.randn(batch_size, fft_size) + np.random.randn(batch_size, fft_size) * 1j
ys = np.fft.fft(xs, axis=1)
(Xs, Ys) = (np.stack([xs.real, xs.imag], axis=2), np.hstack([ys.real, ys.imag]))
print(Xs.shape, Ys.shape)
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(fft_size,2)),
tf.keras.layers.Dense(fft_size*2, use_bias=False)
])
optimizer = tf.keras.optimizers.Adam(amsgrad=True)
model.compile(loss='mean_squared_error', optimizer=optimizer)
history = model.fit(Xs, Ys, epochs=100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment