Detecting If A Number Is Even With Neural Networks

5

1

110b (6)

isEven?

0

101b (5)

Neurons

Array => Number

3

-2

2

4

3

1

5

4

6

-1

0

1

Neurons

Input Layer: NOT Neurons

Neurons

Array => Number

???

5

2

* -1

* 4

-5

8

3

3

function Neuron(weights){
    this.weights = weights
}
Neuron.prototype.process = function(inputs){
    var sum = 0
    for (var i=0; i<inputs.length; i++) {
        sum += inputs[i] * this.weights[i]
    }

    return sum
}
var neuron = new Neuron([4, -2, 6])
var output = neuron.process([1, 0, 1])
// output = 10

0

1

2

2.5

1

0

-3

1.5

1

0

1.5

Is 01b (1) even?

YES!

1.5 > 0.5

Need to adjust weights to get a more correct response!

Training Data

   Number    => isEven
[0, 0, 0] => [1]
[0, 0, 1] => [0]
[0, 1, 0] => [1]
[0, 1, 1] => [0]
[1, 0, 0] => [1]
...

 

Hidden

Layer

5

9

4

0

2

generateTestData(0, 4)

[
    {
        "input":  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        "output": [1]
    },
    {
        "input":  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
        "output": [0]
    },
    {
        "input":  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
        "output": [1]
    },
    {
        "input":  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1],
        "output": [0]
    }
]

0000 0000 0000 0011b === 3

false / not even

var trainingSet = generateTestData(0, 100);

var testSet = generateTestData(10000, 11000);
correctness([w1,  w2, ...], tE) = xx%

correctness([0.2, 2.9, ...], tE) = 60%
 
predictIsEven(number, weights)
==> e.g. 0.17
==> 0.17 < 0.5, prediction is "not even"
exampleIsPredictedCorrectly(example, weights)
==> e.g. true
correctness(weights, trainingExamples)
==> e.g. 60%
correctness([0.2, 2.9, ...], tE) = 60%
correctness([1.2, 1.5, ...], tE) = 74%
correctness([2.1, 0.3, ...], tE) = 23%
correctness([1.3, 0.4, ...], tE) = 54%
const ITERATIONS = 20000

var weights, bestWeights
var bestCorrectness = -1

for (var i=0; i<ITERATIONS; i++){
    weights = getRandomWeights()
    var correctness = getCorrectness(weights, trainingSet)

    if (correctness > bestCorrectness) {
        bestCorrectness = correctness
        bestWeights = cloneObject(weights)
    }
}
$ node is-even.js 

New best correctness in training set: 47%
Correctness in test set:  45%

New best correctness in training set: 50%
Correctness in test set:  50%

...

New best correctness in training set: 90%
Correctness in test set:  75%

New best correctness in training set: 95%
Correctness in test set:  54%

New best correctness in training set: 96%
Correctness in test set:  80%

Figure out what kind of changes to make

Make small random weight changes

 

Improvements

Backpropagation

0

1

2

2.5

1

0

-3

1.5

1

0

1.5

Is 01b (1) even?

YES

1.5 > 0.5

1

.7

1

1 > 0.5

0.7

0.7

0.3 < 0.5

NO

0.6

.5

0.3

0.7 > 0.5

0.5

Using neural nets to recognize handwritten digits

neuralnetworksanddeeplearning.com/chap1.html

Full Code

bit.ly/simple-nn

LeedsJS: Detecting if a number is even with neural networks

By Matt Zeunert

LeedsJS: Detecting if a number is even with neural networks

  • 1,057