• Stars
    star
    237
  • Rank 169,885 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Clean implementation of feed forward neural networks

Build Status Code Climate Documentation

Layered

This project aims to be a clean and modular implementation of feed forward neural networks. It's written in Python 3 and published under the MIT license. I started this project in order to understand the concepts of deep learning. You can use this repository as guidance if you want to implement neural networks what I highly recommend if you are interested in understanding them.

Instructions

This will train a network with 1.3M weights to classify handwritten digits and visualize the progress. After a couple of minutes, the error should drop below 3%. To install globally, just skip the first command. Solutions to all reported problems can be found in the troubleshooting section.

virtualenv . -p python3 --system-site-packages && source bin/activate
pip3 install layered
curl -o mnist.yaml -L http://git.io/vr7y1
layered mnist.yaml -v

Problem Definition

Learning problems are defined in YAML files and it's easy to create your own. An overview of available cost and activation functions is available a few sections below.

dataset: Mnist
cost: CrossEntropy
layers:
- activation: Identity
  size: 784
- activation: Relu
  size: 700
- activation: Relu
  size: 700
- activation: Relu
  size: 400
- activation: Softmax
  size: 10
epochs: 5
batch_size: 32
learning_rate: 0.01
momentum: 0.9
weight_scale: 0.01
weight_decay: 0
evaluate_every: 5000

Command Line Arguments

layered [-h] [-v] [-l weights.npy] [-s weights.npy] problem.yaml
Short Long Description
-h --help Print usage instructions
-v --visual Show a diagram of trainig costs and testing error
-l --load Path to load learned weights from at startup
-s --save Path to dump the learned weights at each evaluation

Contribution

Optionally, create a virtual environment. Then install the dependencies. The last command is to see if everything works.

git clone https://github.com/danijar/layered.git && cd layered
virtualenv . -p python3 --system-site-packages && source bin/activate
pip3 install -e .
python3 -m layered problem/modulo.yaml -v

Now you can start playing around with the code. For pull requests, please squash the changes to a single commit and ensure that the linters and tests are passing.

python setup.py test

If you have questions, feel free to contact me.

Advanced Guide

In this guide you will learn how to create and train models manually rather than using the problem definitions to gain more insight into training neural networks. Let's start!

Step 1: Network Definition

A network is defined by its layers. The parameters for a layer are the amount of neurons and the activation function. The first layer has the identity function since we don't want to already modify the input data before feeding it in.

from layered.network import Network
from layered.activation import Identity, Relu, Softmax

num_inputs = 784
num_outputs = 10

network = Network([
    Layer(num_inputs, Identity),
    Layer(700, Relu),
    Layer(500, Relu),
    Layer(300, Relu),
    Layer(num_outputs, Softmax),
])

Step 2: Activation Functions

Function Description Definition Graph
Identity Don't transform the incoming data. That's what you would expect at input layers. x Identity
Relu Fast non-linear function that has proven to be effective in deep networks. max(0, x) Relu
Sigmoid The de facto standard activation before Relu came up. Smoothly maps the incoming activation into a range from zero to one. 1 / (1 + exp(-x)) Sigmoid
Softmax Smooth activation function where the outgoing activations sum up to one. It's commonly used for output layers in classification because the outgoing activations can be interpreted as probabilities. exp(x) / sum(exp(x)) Softmax

Step 3: Weight Initialization

The weight matrices of the network are handed to algorithms like backpropagation, gradient descent and weight decay. If the initial weights of a neural network would be zero, no activation would be passed to the deeper layers. So we start with random values sampled from a normal distribution.

from layered.network import Matrices

weights = Matrices(network.shapes)
weights.flat = np.random.normal(0, weight_scale, len(weights.flat))

Step 4: Optimization Algorithm

Now let's learn good weights with standard backpropagation and gradient descent. The classes for this can be imported from the gradient and optimization modules. We also need a cost function.

from layered.cost import SquaredError
from layered.gradient import Backprop
from layered.optimization import GradientDecent

backprop = Backprop(network, cost=SquaredError())
descent = GradientDecent()

Step 5: Cost Functions

Function Description Definition Graph
SquaredError The most common cost function. The difference is squared to always be positive and penalize large errors stronger. (pred - target) ^ 2 / 2 Squared Error
CrossEntropy Logistic cost function useful for classification tasks. Commonly used in conjunction with Softmax output layers. -((target * log(pred)) + (1 - target) * log(1 - pred)) Cross Entropy

Step 6: Dataset and Training

Datasets are automatically downloaded and cached. We just iterate over the training examples and train the weights on them.

from layered.dataset import Mnist

dataset = Mnist()
for example in dataset.training:
    gradient = backprop(weights, example)
    weights = descent(weights, gradient, learning_rate=0.1)

Step 7: Evaluation

Finally, we want to see what our network has learned. We do this by letting the network predict classes for the testing examples. The strongest class is the model's best bet, thus the np.argmax.

import numpy as np

error = 0
for example in dataset.testing:
    prediction = network.feed(weights, example.data)
    if np.argmax(prediction) != np.argmax(example.target):
        error += 1 / len(dataset.testing)
print('Testing error', round(100 * error, 2), '%')

Troubleshooting

Failed building wheel

You can safely ignore this messages during installation.

Python is not installed as a framework

If you get this error on Mac, don't create a virtualenv and install layered globally with sudo pip3 install layered.

Crash at startup

Install or reinstall python3-matplotlib or equivalent using your package manager. Check if matplotlib works outside of the virtualenv.

import matplotlib.pyplot as plt
plt.plt([1, 2, 3, 4])
plt.show()

Ensure you create your virtualenv with --system-site-packages.

Did you encounter another problem?

Please open an issue.

More Repositories

1

handout

Turn Python scripts into handouts with Markdown and figures
Python
1,994
star
2

dreamerv2

Mastering Atari with Discrete World Models
Python
770
star
3

dreamerv3

Mastering Diverse Domains through World Models
Python
603
star
4

dreamer

Dream to Control: Learning Behaviors by Latent Imagination
Python
456
star
5

crafter

Benchmarking the Spectrum of Agent Capabilities
Python
279
star
6

mindpark

Testbed for deep reinforcement learning
Python
161
star
7

daydreamer

DayDreamer: World Models for Physical Robot Learning
Jupyter Notebook
141
star
8

director

Deep Hierarchical Planning from Pixels
Python
60
star
9

embodied

Fast reinforcement learning research
Python
50
star
10

ninjax

General Modules for JAX
Python
45
star
11

computer-game

Data-oriented voxel game engine
C++
37
star
12

elements

Building blocks for productive research
Python
36
star
13

crafter-baselines

Docker containers of baseline agents for the Crafter environment
Python
25
star
14

sets

Read datasets in a standard way
Python
19
star
15

diamond_env

Standardized Minecraft Diamond Environment for Reinforcement Learning
Python
18
star
16

voxel-smoothing-2d

Orientation independent bรฉzier smoothing of voxel grids
C++
17
star
17

course-machine-intelligence-2

Jupyter Notebook
13
star
18

npgame

Write simple games in Numpy!
Python
12
star
19

dotfiles

My Linux and Mac configuration
Perl
12
star
20

semantic

Python
10
star
21

training-py

My solutions to programming puzzles
Python
8
star
22

imptools

Tools for improving Python imports
Python
8
star
23

bridgewalk

Visual reinforcement learning benchmark for controllability
Python
6
star
24

cowherd

Partially-observed visual reinforcement learning domain
Python
6
star
25

definitions

Load and validate YAML definitions against a schema
Python
5
star
26

map-pdf

Generate printable PDF documents from Leaflet maps
JavaScript
4
star
27

modurale

Modular real time engine for computer graphics applications
CMake
4
star
28

seminar-knowledge-mining

Wikimedia image classification and suggestings for article authors
Python
3
star
29

couse-ml-stanford

Programming assignments for the Stanford Machine Learning course by Andrew Ng
MATLAB
3
star
30

invoicepad

Freelancer solution covering time tracking, invoice generation and archiving
JavaScript
3
star
31

teleport

Efficiently send large arrays across machines
Python
2
star
32

training-ml

Python
2
star
33

chunkedfile

Save file writes into multiple chunks
Python
1
star
34

notebook-big-data

Jupyter Notebook
1
star
35

course-ml-fuberlin

Python
1
star
36

bookmarks-switcher

Chrome plugin to select which bookmarks folder to show as the bookmarks bar
JavaScript
1
star
37

training-cpp

My solutions to programming puzzles
C++
1
star
38

scope

Metrics logging and analysis
Python
1
star
39

jumper

Platformer and puzzle solving game written in Python
Python
1
star