Single layer neural networks
To become comfortable using neural networks it will be helpful to start with a simple approximation of a function.
You'll train a neural network to approximate a mapping between an input, x
, and an output, y
. They are related by the square root function, i.e. \(y = \sqrt{x}\).
The input vector x
is given to you. You'll first compute the square root of x
using Numpy's sqrt()
function, generating the output series y
. Then you'll create a simple neural network and train the network on the x
series.
After training, you'll then plot both the y
series and the output of the neural network, to see how closely the network approximates the square root function.
The Sequential
and Dense
objects from the Keras library are also available in your workspace.
This exercise is part of the course
Quantitative Risk Management in Python
Exercise instructions
- Create the output training values using Numpy's
sqrt()
function. - Create the neural network with one hidden layer of 16 neurons, one input value, and one output value.
- Compile and fit the neural network on the training values, for 100 epochs
- Plot the training values (in blue) against the neural network's predicted values.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create the training values from the square root function
y = np.____(x)
# Create the neural network
model = Sequential()
model.____(Dense(16, input_dim=1, activation='relu'))
model.____(____(1))
# Train the network
model.____(loss='mean_squared_error', optimizer='rmsprop')
model.____(x, y, epochs=100)
## Plot the resulting approximation and the training values
plt.plot(x, y, x, model.____(x))
plt.show()