Code Monkey home page Code Monkey logo

cnn-in-numpy's Introduction

CNN-in-NumPy

The project serves a purpose of demystifying internal CNN mechanisms of forward and back propagation.

For this purpose we used multiple NumPy functions to train the CNN model on the keras MNIST dataset.

The idea was to (+/-) replicate the tensorflows model of the given pipeline:

    model = keras.Sequential()
    model.add(
        keras.layers.Conv2D(filters=32, kernel_size=(3, 3),
        activation="relu", input_shape=(28, 28, 1))
    )
    model.add(
        keras.layers.Conv2D(filters=64, kernel_size=(3, 3),
        activation="relu")
    )
    model.add(keras.layers.MaxPool2D(pool_size=(2, 2)))
    model.add(keras.layers.Flatten())
    model.add(keras.layers.Dense(128, activation="relu"))
    model.add(keras.layers.Dense(10, activation="softmax"))
              
    model.compile(
        loss='categorical_crossentropy',
        optimizer=keras.optimizers.Adadelta(learning_rate=1),
        metrics='accuracy'
    )

Simplifications / constraints / assumptions

  1. The model uses simple gradient descent optimizer (which means constant learning rate)
  2. For now the model is static, therefore adjusting of layers order is limited
  3. Handles one image at the time
  4. Doesn't include bias matrices

Sample run results

Model was initially run on 625 training sample (500 for actual training, and 125 for validation) on 10 epochs. Plots below show the history of loss and accuracy for this approach. The model clearly overfits due to the small sample size, but it can be seen that loss is minimized between epochs.

To-be-done

  • optimization by translating to machine code (numba / cython)
  • Adding Dropout layers
  • Making one universal convolution function with adjustable parameters
  • Including bias matrices
  • Including batch size > 1
  • Including easier modification of parameters such as number of hidden layers / number of convolution - maxpool sequences
  • Further optimization of functions

Collaborators

References

License

MIT License | Copyright (c) 2021 Jan Androsiuk

cnn-in-numpy's People

Contributors

janandrosiuk avatar

Watchers

 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.