Code Monkey home page Code Monkey logo

first-neural-network's Introduction

My first Neural Networks built from scratch

Presentation and dependencies

The simple_nn.py Python code is a feedforward neural network model, with one hidden layer, written from scratch using only two packages numpy and mnist to download the training and testing sets.

The one_hidden_layer_nn.py is an improved version of the code using object-oriented programming. The structure of the network is the same. Additional functions are introduced to evaluate the performance of the network. Moreover this version does not use the mnist package, but instead the raw files of the MNIST database (that can be downloaded at http://yann.lecun.com/exdb/mnist/) are used.

Even better, custom_nn.py can create and train feedforward neural networks with a custom number of layers (depth) each with a custom size (width). The code used for the forward and backpropagation is also improved to make the training faster.

The networks are trained to classify handwritten digits using the MNIST database. Here is an extract of some MNIST images:

The simple_nn_test.ipynb contains a demo of how to use the simple_nn.py file to train and test the network's performance. There are also explanations of how the network works and is trained using gradient descent. An illustration of the output of the network:

For the custom_nn_test.ipynb, the dimensions of the network tested are 784 (for the input), 128 (first hidden layer), 32 (second hidden layer) and 10 (output). Here are some results as well as a test on my own handwritten digit:

Structure of the simple_nn.py

This is a fully connected forward propagating network with 3 layers :

  • the input layer has 784 nodes (28 x 28 input images are flattened)
  • the hidden layer has 16 nodes
  • the output layer has 10 nodes

Activation functions used:

  • Sigmoid for the hidden layer
  • Softmax for the output layer

The loss is calculated with cross entropy.

Usage guide of the simple_nn.py functions

  1. Run the train() function to train the neural network. This function executes 200 iterations. For each iteration a batch of 32 images is processed. At the end of each iteration the weights and biases are updated.

  2. Run the accuracy() function to see the network's performance.

  3. Feel free to change the network's parameters and try to improve accuracy.

first-neural-network's People

Contributors

huber2 avatar penpy avatar

Stargazers

 avatar

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.