Last active
March 2, 2020 18:37
-
-
Save 0xfe/5a029d815b9a5f22985933cee1a71562 to your computer and use it in GitHub Desktop.
Neural Network to learn the Discrete Fourier Transform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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