Machine learning framework written from scratch.
The goal is to keep the code simple and readable (the core library is ~300 lines) and written mostly in Python (convolution is implemented in C to avoid a major bottleneck).
git clone
the project somewherepython setup.py install
to build C extensions and install./setup_data
to download datasets (if you want to run the examples). This will download about 175 MB.
mnist_fc_nn.py
trains a fully connected neural network on MNIST data. You should see ~96% accuracy.mnist_cnn.py
trains a convnet on MNIST data. You should see 98% or higher accuracy.
The ml.nn
module is used to create and train neural networks.
The module contains these classes:
-
Net
– neural network composed of layers- Constructor
Net(layer)
– creates a networks from an array of layers. The last layer should be a loss layer.
- Methods
forward_pass(input[, layers])
– feedinput
into the bottom layer and propagate forward, returning the result of the last layer. It propagates throughlayers
, if given, or defaults to the layers given to the constructor.backward_pass(top_diff=1)
- perform backpropagation through the network, giving the top layer the gradienttop_diff
(1 if omitted). Returns the gradient of the input received during the forward pass.forward_pass
must be called first!train_gd(X, Y, lr, iters)
– train the network on a dataset with inputsX
and outputsY
via gradient descent withiters
iterations and learning ratelr
.accuracy(X, Y)
– test the accuracy of the network on a dataset with inputsX
and outputsY
.
- Constructor
-
WeightLayer
– fully connected layer- Constructor
WeightLayer(w, b)
- create a fully connected layer with weightsw
and biasesb
(both numpy arrays).WeightLayer.create_transition(units_bottom, units_top, init_type='random')
– creates a fully connected layer that transitions from a layer withunits_bottom
neurons tounits_top
neurons.init_type
describes how the weights are initialized and can be either'random'
,'xavier'
, or'xavier_relu'
- Constructor
-
ConvLayer
- convolution layer- Constructor
ConvLayer(in_depth, nfilters, fsize)
– creates a convolution layer that accepts a volume with depthin_depth
and and outputs a volume with depthnfilters
using afsize
xfsize
sized kernel.
- During the forward pass, a
ConvLayer
should be given a volume in the form of a numpy array with shape(depth, height, width)
.
- Constructor
-
PoolLayer
- pooling layer- Constructor
PoolLayer(block_size, pool_type='max')
– creates a pooling layer that downsamples the input volume usingblock_size
xblock_size
blocks. Currently only max-pooling is implemented.
- Constructor
-
SigmoidLayer
- sigmoid activiation layer- Constructor:
SigmoidLayer()
- Constructor:
-
TanhLayer
- tanh activation layer- Constructor:
TanhLayer()
- Constructor:
-
ReLULayer
- ReLU (REctified Linear Unit) activation layer- Constructor:
ReLULayer()
- Constructor:
-
UnfurlLayer
- unfurls an arbitrarily dimensioned input into a column vector- Constructor:
UnfurlLayer()
- This layer is useful for feeding a convolution volume into a fully connected layer
- Constructor:
-
SoftmaxLayer
- applies the softmax function so that outputs sum to one.- Constructor:
SoftmaxLayer()
- Constructor:
-
SquaredLoss
- squared loss between input and target- Constructor:
SquaredLoss()
- The target value must be set using the
target
attribute
- Constructor:
-
CrossEntropyLoss
- cross entropy loss- Constructor:
CrossEntropyLoss()
- The target value must be set using the
target
attribute
- Constructor:
-
DropoutLayer
- dropout layer (randomly kills neurons)- Constructor
DropoutLayer(p)
– creates a dropout layer with probablityp
of any given neuron dropping out (i.e.p = 0.5
will kill about half of the input neurons).
- Constructor
All layer objects must have these two methods implemented:
forward(bottom)
– feedsbottom
into the layer and returns the output.backward(top_diff)
feeds the gradienttop_diff
into the layer and returns the bottom's gradient.