Sinusoid example

A sinusoidal wave is a simple yet instructive function to fit, as it allows to investigate how the performance of optimization algorithms varies when the frequency is changed over a fixed domain. We start by importing neuralfit (for fitting), numpy (for arrays) and matplotlib (for plotting).

import neuralfit as nf
import matplotlib.pyplot as plt
import numpy as np

The next step is to define the dataset, in this case the sine wave. We want to fit \sin(x) on the domain x\in[0,2\pi], exactly one period. For this example we will linearly sample 1000 datapoints in the domain, but feel free to change the sampling method or the number of points.

x = np.linspace(0, 2*np.pi, 1000).reshape(-1,1)
y = np.sin(x)

Now we can create the NeuralFit model. We specify the inputs and outputs, and initialize it with 2 hidden neurons (4 minus the input and output) in order to avoid initial local minima. Afterwards the model can be compiled, specifying the Alpha optimizer and the mean-squared error loss function. Next to the loss, we would also like to model the size of the best-performing network during training so we specify the size monitor.

model = nf.Model(inputs=1, outputs=1, size=4)
model.compile(optimizer='alpha', loss='mse', monitors=['size'])

Now that the model and dataset have been set up, we can simply call model.evolve to train the model! We specify 1000 epochs, which should be (on average) plenty of time to get good results.

model.evolve(x, y, epochs=1000)

Note that neuro-evolution is typically much more sensitive to the initial weights compared to standard (backprop) optimizers, so evolution might not always converge! But once you have a good run (mse<0.01) you can ask the model for predictions and visualize them using matplotlib.

# Get model predictions
y_hat = model.predict(x)

# Plot results
plt.plot(x, y, label='True', color='k', linestyle='--')
plt.plot(x, y_hat, label='Predicted',color='#52C560', linewidth=2)
plt.show()

If all went well, you should get a plot similar to the one below! If you want an extra challenge, try varying the frequency of the sinusoid to see how it affects the performance of the model.