| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
ML.NN
Contents
Description
Simple neural network implementation, for learning purposes only.
- type ActivationFunction = R -> R
- type ActivationFunctionDerivative = R -> R
- data Layer = Layer {
- layerBiases :: !(Vector R)
- layerWeights :: !(Matrix R)
- data Network = Network {
- networkLayers :: [Layer]
- randLayer :: RandomGen g => Int -> Int -> Rand g Layer
- randNetwork :: RandomGen g => [Int] -> Rand g Network
- feedForward :: ActivationFunction -> Vector R -> Layer -> Vector R
- runNetwork :: Network -> ActivationFunction -> Vector R -> Vector R
- data TrainingConfig = TrainingConfig {}
- data Sample = Sample {
- sampleInput :: !(Vector R)
- sampleExpectedOutput :: !(Vector R)
- data Gradient = Gradient {
- gradientNablaB :: !(Vector (Vector R))
- gradientNablaW :: !(Vector (Matrix R))
- type CostDerivative = Vector R -> Vector R -> Vector R
- sgd :: TrainingConfig -> Int -> Int -> Vector Sample -> Network -> IO Network
- gradientDescentCore :: TrainingConfig -> Vector Sample -> Network -> Network
- mse' :: CostDerivative
- computeZsAndAs :: ActivationFunction -> Network -> Vector R -> ([Vector R], [Vector R])
Data types
type ActivationFunction = R -> R Source
An activation function for a neuron.
type ActivationFunctionDerivative = R -> R Source
The derivative of a neuron activation function.
A layer in a neural network is a bias vector and weight matrix.
Let n be the number of neurons in the layer and m be the number
of inputs to the layer. Then layerBiases is a vector in Rⁿ and
layerWeights is an nxm real matrix.
Constructors
| Layer | |
Fields
| |
A network is a list of layers.
Constructors
| Network | |
Fields
| |
Network initialization
Arguments
| :: RandomGen g | |
| => Int | Number of inputs into the layer. |
| -> Int | Number of neurons in the layer. |
| -> Rand g Layer | Randomly generated layer. |
Generate a randomly initialized layer in a neural network.
randNetwork :: RandomGen g => [Int] -> Rand g Network Source
Generate a random neural network given the size of each layer.
For example, [3,2,4] will generate a neural network with 3 input
neurons, 1 hidden layer with 2 neurons and 4 output neurons.
Running networks
Arguments
| :: ActivationFunction | Neuron activation function. |
| -> Vector R | Input to the layer. |
| -> Layer | Layer to run. |
| -> Vector R | Output from the layer. |
Feed the output from a previous layer to the next layer.
Arguments
| :: Network | Network to run. |
| -> ActivationFunction | Neuron activation function. |
| -> Vector R | Network input. |
| -> Vector R | Network output. |
Run a neural network.
Training networks
data TrainingConfig Source
Configuration for training a network.
Constructors
| TrainingConfig | |
Fields
| |
A training sample.
Constructors
| Sample | |
Fields
| |
The gradient of the network.
Constructors
| Gradient | |
Fields
| |
type CostDerivative = Vector R -> Vector R -> Vector R Source
A vectorized function which returns ∂Cₓ/∂a.
The first parameter is the output activation, the second parameter is the expected output.
Arguments
| :: TrainingConfig | |
| -> Int | epochs |
| -> Int | mini-batch size |
| -> Vector Sample | |
| -> Network | |
| -> IO Network |
Train the neural network using stochastic gradient descent.
gradientDescentCore :: TrainingConfig -> Vector Sample -> Network -> Network Source
Update the network's weights and biases by applying gradient descent for the given sample input.
Cost function derivatives
The derivative of the mean squared error cost function.
Internal (exposed for testing)
computeZsAndAs :: ActivationFunction -> Network -> Vector R -> ([Vector R], [Vector R]) Source
Return z and activation values for each layer in the network.
The values are returned in reverse order for use by the backpropogation algorithm. We the State monad so it's more explicit that the output activation of one layer is the input to the next.