GithubHelp home page GithubHelp logo

crazydonkey200 / minimal-nn Goto Github PK

View Code? Open in Web Editor NEW
72.0 2.0 18.0 748 KB

A minimal implementation of neural network for MNIST experiment. Used as an exercise to help understanding Backpropagation by implementing it in NumPy.

Python 100.00%

minimal-nn's Introduction

minimal-nn

A minimal implementation of neural network for MNIST experiment. Used as an exercise to understand Backpropagation by implementing it in NumPy.

Dependencies

  • Python 2.7
  • numpy
  • matplotlib

Instructions

Neural networks have two modes: inference/prediction and learning/training. They are implemented as forward propagation and backward propagation (backpropagation for short).

fnn.py contains a minimal implementation of multi-layer feedforward neural network. The main class is FNN that holds a list of layers, and defines the high level iterative process for forward and backward propagation. Class Layer implements each layer in the neural network. Class GradientDescentOptimizer implements an optimizer for training the neural network. The utility functions at the end implements different activation functions, loss functions and their gradients. Read through fnn.py to get an overview of the implementation. Like most effcient implementations of neural network, we are using minibatch gradient descent instead of stochastic gradient descent, see this video to learn more.

  • Forward propagation: the core of inference/prediction in NN. In this part, you need to complete the forward method in class Layer in fnn.py (search for Question 1 to see the instructions). (2 lines of code)

  • Backpropagation: the core of learning/training in NN. In this part, you need to complete the backward method in class Layer in fnn.py (search for Question 2 to see the instructions). (4 lines of code)

Read this notes on intuition and implementation tips for Backpropagation. Backprop in practice: Staged computation and Gradients for vectorized operations sections are especially helpful with good examples and practical tips.

First, download the MNIST dataset by running

python get_mnist_data.py

To test your implementation, run

python test_fnn.py

There are two tests test_forwardprop and test_backprop. When your implementation passes both of them, run

python mnist_experiment.py

to train a small deep neural network with 2 hidden layers (containing 128 and 32 RELU units each) for handwritten digits recognition using MNIST dataset. The accuracy should be around 99% on training set and around 97% on validation and test set.

To demonstrate the effect of learning, 100 randomly selected test images will be shown with true labels (black on top left corner), predictions before training (red on bottom right corner), and predictions after training (blue on bottom left corner). See the figure below as an example. You can see that the predictions improve from random guess to almost perfect. Yes, it learns :)

Example figure

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.