A purely functional Haskell implementation of stochastic gradient descent for deep feed-forward neural networks.
Why functional programming for neural networks? The goal of this library is mathematical readability: the close coupling between functional programming and mathematical expressions allows to manipulate neural networks (or anything that can be expressed mathematically) by declaring expressions, rather than by implementing algorithms.
Approaching a problem by declaring its solution instead of implementing an algorithm to find that solution allows the programmer to be succint and clear. For example, here's the function that computes the output of a neural network for a given input:
output :: (Floating t) => Matrix t -> Network t -> Matrix t
output = foldl activationHere, output is a function of two arguments: an input Matrix and a Network. output is defined as a left fold across the layers of the network, with the input matrix as the initial state of the accumulator and the activation function as the combining operation.
The example.hs program shows an example of how to use this library.
The DeepNN module exports data structures and functions to create, train, and use deep feed-forward neural networks:
Networkis a data type that represents a deep neural network;CostFunctionis the class of cost functions, whose available instances areQuadCostandCrossEntCost;outputis the output of a neural network, given some input;sgdUpdatesandsgdUpdates'are an infinite list of networks whose parameters are updated with SGD throughout (infinite) epochs, respectively with and without re-shuffling the dataset at each epoch;costis the cost of a neural network on a dataset, w.r.t. a given cost function;binAccuracyandcatAccuracyare the accuracies (resp. binary and categorical) of a neural network on a dataset;randNetis a network with randomDoubleweights sampled from a normal distribution with given mean and standard deviation.
The Dataset module currently exports the following functions:
fromCsvis a dataset of 1DDoublesamples, taken from a given CSV file;shuffledis a random permutation of the elements in a list, given a random number generator;foreachis the result of applying a function to each element of a matrix;standardizedis a list of training samples that have been standardized.
To use a module (such as DeepNN for example), have DeepNN.hs in the search path of GHC, then import DeepNN inside your Haskell program. See the example program.
DeepNN depends on several built-in Haskell modules, but also on the Data.matrix module: to install it, type cabal install matrix in a shell.