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 clonethe project somewherepython setup.py installto build C extensions and install./setup_datato download datasets (if you want to run the examples). This will download about 175 MB.
mnist_fc_nn.pytrains a fully connected neural network on MNIST data. You should see ~96% accuracy.mnist_cnn.pytrains 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])– feedinputinto 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_passmust be called first!train_gd(X, Y, lr, iters)– train the network on a dataset with inputsXand outputsYvia gradient descent withitersiterations and learning ratelr.accuracy(X, Y)– test the accuracy of the network on a dataset with inputsXand outputsY.
- Constructor
-
WeightLayer– fully connected layer- Constructor
WeightLayer(w, b)- create a fully connected layer with weightswand 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_bottomneurons tounits_topneurons.init_typedescribes 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_depthand and outputs a volume with depthnfiltersusing afsizexfsizesized kernel.
- During the forward pass, a
ConvLayershould 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_sizexblock_sizeblocks. 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
targetattribute
- Constructor:
-
CrossEntropyLoss- cross entropy loss- Constructor:
CrossEntropyLoss() - The target value must be set using the
targetattribute
- Constructor:
-
DropoutLayer- dropout layer (randomly kills neurons)- Constructor
DropoutLayer(p)– creates a dropout layer with probablitypof any given neuron dropping out (i.e.p = 0.5will kill about half of the input neurons).
- Constructor
All layer objects must have these two methods implemented:
forward(bottom)– feedsbottominto the layer and returns the output.backward(top_diff)feeds the gradienttop_diffinto the layer and returns the bottom's gradient.