Learning

AI 101

Setting the Stage

  • We’ve explored the perceptron
  • We’ve used binary classification to different odds and evens.
  • We approach multi-class classification.
  • And finally… we placed the entire framework within a matrix.

Setup

import numpy as np
one = [0,0,0,0,1,0,0,0,0]
two = [0,0,1,0,0,0,1,0,0]
thr = [0,0,1,0,1,0,1,0,0]
fou = [1,0,1,0,0,0,1,0,1]
fiv = [1,0,1,0,1,0,1,0,1]
six = [1,0,1,1,0,1,1,0,1]
dice = np.array([one, two, thr, fou, fiv, six])

Recall

Representing Reality

  • How does a computer “see” a simple object?
  • Let’s take a standard six-sided die.
  • We can represent the face of a die as a 3x3 grid.
  • Or, alternatively, a 1x9 (or 9x1) “vector”

NumPy

  • We used NumPy to perform element-wise multiplication over vectors.
  • This is also called the “Hadamard product” or “element-wise product”.
  • We step through the example from the lab, briefly.

Is One

  • Last week, we found it was easy enough to classify one.
  • Briefly, we looked for a center dot with a positive weight, and gave everything else a negative weight.
  • Then we multiplied “element-wise” the visual data (a vector of length 9) by the weights (a vector of length 9).

\[ \sum_{i=1}^{n} w_i \times x_i \]

Is One Vector

  • We used, perhaps, this “is one?” vector.
    • Recall, we cannot include spaces in our names of things in Colab.
    • We use “single equals assignment” to assign a variable to some name.
    • In this case, the variable is the “is one?” vector.
is_one_vector = np.array([-1,-1,-1,-1,1,-1,-1,-1,-1])

Compare

  • Find out which die we have in a single line using vector arithmetic.
0 < np.sum(dice * is_one_vector, 1)
array([ True, False, False, False, False, False])

Matrices

  • Imagine that instead of having six dice that we want to check to see if they are one.
  • We instead have one die and we wish to see what value it represents.
    • We represent the die as a 1 by 9 (or 9 by 1) vector
    • We represent its value (1 to 6) as a 1 by 6 (or 6 by 1) vector.
    • We can perform a single matrix multiplication using a 9 by 6 to get the result.

For example

  • We wish to find out what “class” (what number) fiv belongs to.
  • We would start with fiv
fiv = np.array(fiv)
fiv
array([1, 0, 1, 0, 1, 0, 1, 0, 1])
  • We would want to get back something with a 1 in the fiveth position, and zeroes elsewhere.
[0, 0, 0, 0, 1, 0,]
[0, 0, 0, 0, 1, 0]

That matrix

  • This matrix is the precise matrix made in the lab.
multi = np.array([
    [-1/1, -1/1, -1/1, -1/1,  1/1, -1/1, -1/1, -1/1, -1/1],
    [-1/2, -1/2,  1/2, -1/2, -1/2, -1/2,  1/2, -1/2, -1/2],
    [-1/3, -1/3,  1/3, -1/3,  1/3, -1/3,  1/3, -1/3, -1/3],
    [ 1/4, -1/4,  1/4, -1/4, -1/4, -1/4,  1/4, -1/4,  1/4],
    [ 1/5, -1/5,  1/5, -1/5,  1/5, -1/5,  1/5, -1/5,  1/5],
    [ 0/6,  3/6,  0/6,  3/6, -6/6,  3/6,  0/6,  3/6,  0/6],
])

Compare to 1

  • Once again, we can simply by just seeing if there’s enough “weight” to make a neuron fire, or not.
1 <= np.sum(fiv * multi,1)
array([False, False, False, False,  True, False])
  • Only the neuron representing “five” will fire!

Step Back

Collect Our Thoughts

  • Is this intelligent:
print(multi)
[[-1.         -1.         -1.         -1.          1.         -1.
  -1.         -1.         -1.        ]
 [-0.5        -0.5         0.5        -0.5        -0.5        -0.5
   0.5        -0.5        -0.5       ]
 [-0.33333333 -0.33333333  0.33333333 -0.33333333  0.33333333 -0.33333333
   0.33333333 -0.33333333 -0.33333333]
 [ 0.25       -0.25        0.25       -0.25       -0.25       -0.25
   0.25       -0.25        0.25      ]
 [ 0.2        -0.2         0.2        -0.2         0.2        -0.2
   0.2        -0.2         0.2       ]
 [ 0.          0.5         0.          0.5        -1.          0.5
   0.          0.5         0.        ]]

Our Task

  • We were trying to do a computer vision task.
  • Specifically, we wished to classify dice.

Snake eyes dice

Encoding

  • To do so, we recognized we could view dice with as few as nine (9) “sensory neurons”.
1 2 3
4 5 6
7 8 9
  • Each of these is either 0 or 1 or perhaps True or False etc.

Vectors

  • We then recognized we could place these in any order, and in a single “dimension”.
    • We termed this a vector.
1 2 3 4 5 6 7 8 9

Graphs

  • We then discussed that these vectors could be treated as part of a graph.
    • The top, input layer representing the visual data.
    • The bottom, output layer representing the classification

SudokuBipartite cluster_cells C22 (2,2) D06 6 C22--D06 D05 5 C22--D05 D04 4 C22--D04 D03 3 C22--D03 D02 2 C22--D02 D01 1 C22--D01 C21 (2,1) C21--D06 C21--D05 C21--D04 C21--D03 C21--D02 C21--D01 C20 (2,0) C20--D06 C20--D05 C20--D04 C20--D03 C20--D02 C20--D01 C12 (1,2) C12--D06 C12--D05 C12--D04 C12--D03 C12--D02 C12--D01 C11 (1,1) C11--D06 C11--D05 C11--D04 C11--D03 C11--D02 C11--D01 C10 (1,0) C10--D06 C10--D05 C10--D04 C10--D03 C10--D02 C10--D01 C02 (0,2) C02--D06 C02--D05 C02--D04 C02--D03 C02--D02 C02--D01 C01 (0,1) C01--D06 C01--D05 C01--D04 C01--D03 C01--D02 C01--D01 C00 (0,0) C00--D06 C00--D05 C00--D04 C00--D03 C00--D02 C00--D01

Edges

  • Edges in this graph could connect sensed dots to numeric meaning.
    • Middle square to odd numbers.

SudokuBipartite cluster_cells C22 (2,2) D06 6 C22--D06 D05 5 C22--D05 D04 4 C22--D04 D03 3 C22--D03 D02 2 C22--D02 D01 1 C22--D01 C21 (2,1) C21--D06 C21--D05 C21--D04 C21--D03 C21--D02 C21--D01 C20 (2,0) C20--D06 C20--D05 C20--D04 C20--D03 C20--D02 C20--D01 C12 (1,2) C12--D06 C12--D05 C12--D04 C12--D03 C12--D02 C12--D01 C11 (1,1) C11--D06 C11--D05 C11--D05 C11--D04 C11--D03 C11--D03 C11--D02 C11--D01 C11--D01 C10 (1,0) C10--D06 C10--D05 C10--D04 C10--D03 C10--D02 C10--D01 C02 (0,2) C02--D06 C02--D05 C02--D04 C02--D03 C02--D02 C02--D01 C01 (0,1) C01--D06 C01--D05 C01--D04 C01--D03 C01--D02 C01--D01 C00 (0,0) C00--D06 C00--D05 C00--D04 C00--D03 C00--D02 C00--D01

Weights

  • Some edges may matter more than others.
    • Center is very important for 1
    • Kinda important for 3 and 5 - where corners matter too.

SudokuBipartite cluster_cells C22 (2,2) D05 5 C22--D05 C20 (2,0) C20--D05 D03 3 C20--D03 C11 (1,1) C11--D05 C11--D03 D01 1 C11--D01 C02 (0,2) C02--D05 C02--D03 C00 (0,0) C00--D05

Matrices

  • Then, we note that we can express these weights in a matrix.
multi = np.array([
    [-1/1, -1/1, -1/1, -1/1,  1/1, -1/1, -1/1, -1/1, -1/1],
    [-1/2, -1/2,  1/2, -1/2, -1/2, -1/2,  1/2, -1/2, -1/2],
    [-1/3, -1/3,  1/3, -1/3,  1/3, -1/3,  1/3, -1/3, -1/3],
    [ 1/4, -1/4,  1/4, -1/4, -1/4, -1/4,  1/4, -1/4,  1/4],
    [ 1/5, -1/5,  1/5, -1/5,  1/5, -1/5,  1/5, -1/5,  1/5],
    [ 0/6,  3/6,  0/6,  3/6, -6/6,  3/6,  0/6,  3/6,  0/6],
])

Simpler

  • Trying to make this easier to see and think about.
multi = np.array([
    [ 0,  0,  0,  0,  1,  0,  0,  0,  0],
    ...
    [ 0,  0, .3,  0, .3,  0, .3,  0,  0],
    ...
    [.2,  0, .2,  0, .2,  0, .2,  0, .2],
    ...
])

Lastly

  • Then, we noted we could express a neuron as an object.
class Neuron:

    def __init__(self, weights, bias):
        self.weights = np.array(weights)
        self.bias = bias

    def __repr__(self):
        return "Neuron" + str(self.weights)

    def __mul__(self, other):
        return int(np.sum(self.weights * other) >= self.bias)

Example

  • We can make, say, the neuron that recognizes ⚄
is_fiv = Neuron([ 1/5, -1/5,  1/5, -1/5,  1/5, -1/5,  1/5, -1/5,  1/5], 1)
print(is_fiv)
Neuron[ 0.2 -0.2  0.2 -0.2  0.2 -0.2  0.2 -0.2  0.2]

Try it

  • We can verify the neuron “recognizes” ⚄
is_fiv * fiv
1
  • But nothing else…
is_fiv * thr
0

Learning

My claim

  • I claim this is not intelligent, though it can do an intelligent task (vision).
multi = np.array([
    [-1/1, -1/1, -1/1, -1/1,  1/1, -1/1, -1/1, -1/1, -1/1],
    [-1/2, -1/2,  1/2, -1/2, -1/2, -1/2,  1/2, -1/2, -1/2],
    [-1/3, -1/3,  1/3, -1/3,  1/3, -1/3,  1/3, -1/3, -1/3],
    [ 1/4, -1/4,  1/4, -1/4, -1/4, -1/4,  1/4, -1/4,  1/4],
    [ 1/5, -1/5,  1/5, -1/5,  1/5, -1/5,  1/5, -1/5,  1/5],
    [ 0/6,  3/6,  0/6,  3/6, -6/6,  3/6,  0/6,  3/6,  0/6],
])
  • Rather, I am intelligent, since I made it.

My claim

  • I do, however, claim, that this is pretty close to being intelligent:
is_fiv * fiv
1
  • As a method, it could theoretically recognize anything.
    • Art
    • Love
    • Beauty
    • True

Generalize

  • The trouble is, how do we get the values in the matrix?
  • Somehow, to go from “just arithmetic” to intelligence, we need to learn.
  • This is where machine learning comes into artificial intelligence.

Setup

Our Goals

  • We will:
    • Start with “nothing”
      • An arbitrary matrix with random values.
    • Construct some learning process
      • We will represent this with Colab code, but the ideas are independent of how we write them.
    • Show that we can recognize dice.
      • And therefore (at least theoretically) anything

Example

  • We started with this:
multi = np.array([
    [-1/1, -1/1, -1/1, -1/1,  1/1, -1/1, -1/1, -1/1, -1/1],
    [-1/2, -1/2,  1/2, -1/2, -1/2, -1/2,  1/2, -1/2, -1/2],
    [-1/3, -1/3,  1/3, -1/3,  1/3, -1/3,  1/3, -1/3, -1/3],
    [ 1/4, -1/4,  1/4, -1/4, -1/4, -1/4,  1/4, -1/4,  1/4],
    [ 1/5, -1/5,  1/5, -1/5,  1/5, -1/5,  1/5, -1/5,  1/5],
    [ 0/6,  3/6,  0/6,  3/6, -6/6,  3/6,  0/6,  3/6,  0/6],
])

Size

  • As discussed, to look at:
    • 9 locations, and produce
    • 6 possible characterizations
  • We used a \(9 \times 6\) matrix.
multi.shape
(6, 9)

Layer

  • We would instead view this as what is usually called a layer of neurons.
  • It’s just a collection of neurons, in no particular order.
layer = np.array([
    Neuron([-1/1, -1/1, -1/1, -1/1,  1/1, -1/1, -1/1, -1/1, -1/1], 1),
    Neuron([-1/2, -1/2,  1/2, -1/2, -1/2, -1/2,  1/2, -1/2, -1/2], 1),
    Neuron([-1/3, -1/3,  1/3, -1/3,  1/3, -1/3,  1/3, -1/3, -1/3], 1),
    Neuron([ 1/4, -1/4,  1/4, -1/4, -1/4, -1/4,  1/4, -1/4,  1/4], 1),
    Neuron([ 1/5, -1/5,  1/5, -1/5,  1/5, -1/5,  1/5, -1/5,  1/5], 1),
    Neuron([ 0/6,  3/6,  0/6,  3/6, -6/6,  3/6,  0/6,  3/6,  0/6], 1),
])

View it

  • It looks a bit goofy!
print(layer)
[Neuron[-1. -1. -1. -1.  1. -1. -1. -1. -1.]
 Neuron[-0.5 -0.5  0.5 -0.5 -0.5 -0.5  0.5 -0.5 -0.5]
 Neuron[-0.33333333 -0.33333333  0.33333333 -0.33333333  0.33333333 -0.33333333
   0.33333333 -0.33333333 -0.33333333]
 Neuron[ 0.25 -0.25  0.25 -0.25 -0.25 -0.25  0.25 -0.25  0.25]
 Neuron[ 0.2 -0.2  0.2 -0.2  0.2 -0.2  0.2 -0.2  0.2]
 Neuron[ 0.   0.5  0.   0.5 -1.   0.5  0.   0.5  0. ]]

Randomize

  • Rather than using our own intelligence to create the matrix…
  • …we can randomize everything.
  • Imagine, say, when we first try to understand something.
    • About the same as random guessing.
  • We use np.random.rand and tell it what shape we want.
learn = np.random.rand(6,9)

View it

  • It looks like this:
    • All between 0 and 1 by default.
print(learn)
[[0.84782583 0.99754661 0.37144545 0.35951125 0.51952776 0.91167008
  0.18851714 0.60068732 0.83017473]
 [0.38500627 0.78754125 0.17222118 0.12132224 0.74023843 0.76830758
  0.082289   0.85673835 0.52457145]
 [0.44879353 0.07166859 0.90172392 0.01826042 0.35074145 0.58281399
  0.41051414 0.48591482 0.7055296 ]
 [0.14371779 0.49439823 0.98417158 0.75418984 0.89979416 0.51440135
  0.06060489 0.86794552 0.32507273]
 [0.03575555 0.59445296 0.59595104 0.6717415  0.79543474 0.08331623
  0.17968061 0.95994688 0.0408776 ]
 [0.59737745 0.46468292 0.68213292 0.2598974  0.18333893 0.2499059
  0.12540588 0.53740209 0.0211946 ]]

As Neurons

  • We can make this a bit simpler by considering neurons.
  • We introduce the notion of defaults
  • They would look like this:
    def __init__(self, weights=np.random.rand(9), bias=1):
  • By using single equals assignment within the dunder method declaration
    • Say that three times fast.
  • We can provide arguments to the method that are defaults.

Like This

  • Rewrite neuron, and
class Neuron:

    def __init__(self, weights=np.random.rand(9), bias=1):
        self.weights = np.array(weights)
        self.bias = bias

    def __repr__(self):
        return "Neuron" + str(self.weights)

    def __mul__(self, other):
        return int(np.sum(self.weights * other) >= self.bias)
rand_n = Neuron()
print(rand_n)
Neuron[0.17127433 0.39132689 0.60473972 0.38720399 0.88212299 0.51692685
 0.45581221 0.02576074 0.85801543]

Check it out!

  • Mine:
for die in dice:
    print(is_fiv * die)
0
0
0
0
1
0
  • Random:
for die in dice:
    print(rand_n * die)
0
1
1
1
1
1
  • Random is… not as good!

What happened?

  • Well, we multiplied each of the nine possible places for a dot in a die by something.
  • For each of these six classes, we summed up the nine products
    • The presence of dot multiplied by importance of dot, for all dots.

Examine the results

  • For each prediction, we can determine which row (or column) of the matrix made that prediction.
  • If that prediction is correct, we can “strengthen” the connections.
    • Perhaps increase them by 10% or by .1 or something.
  • If the prediction is incorrect, we can “penalize” the connections.
    • Perhaps decrease them by 10% or by .1 or something.

“Supervised” Learning

  • In this case, we engage in supervised learning.
    • We know the answer.
    • We can compare against the answer.

Learning

We’re ready

  • Now we are ready to go from nothing (random guessing) to something (intelligence)
  • We will go over every classification.
    • If it is the same as the answer key, we will “reward” the row - increasing its weights.
    • If it differs, we will decrease the weights.

Writing it out

  • No real way around using code for this as far as I know.

Writing it out

  • No real way around using code for this as far as I know.
  • We’ll add a new method to our object, call it learn
    • Learn is not a dunder method.
    • Just like a regular function, but…
    • Must take self since it’s a function inside of a neuron.
    def learn(self...

Using learn

  • When we’re done, we’ll use it using “dot notation”.
    • Take the name of the object
    • Suffix it with a “dot” .
    • Append the name of the method
    • Then provide any arguments etc.
rand_n.learn(...
  • We’ll get there!

Using dunder methods

  • It is possible to use dunder methods the same way, it is just extremely ugly.
x = 2
x.__add__(2)
4

Writing it out

  • No real way around using code for this as far as I know.
  • We’ll add a new method to our object, call it learn
  • It will:
    • Take some perceived vector of values
    def learn(self, perceived...

Writing it out

  • No real way around using code for this as far as I know.
  • We’ll add a new method to our object, call it learn
  • It will:
    • Take some perceived vector of values, and
    • Take what the right answer is
    def learn(self, perceived, answer):

Writing it out

  • No real way around using code for this as far as I know.
  • We’ll add a new method to our object, call it learn
  • It will:
    • Take some perceived vector of values, and
    • Take what the right answer is, and then
    • Update it’s weights.

Updating Weights

  • This isn’t too bad.
  • We’ll
    • Compute how “excited” the neuron would be with the current weights.
    def learn(self, perceived, answer):
        self * perceived

Updating Weights

  • This isn’t too bad.
  • We’ll
    • See if we’re already doing it right, using double equals comparison
    def learn(self, perceived, answer):
        if answer == self * perceived

Updating Weights

  • This isn’t too bad.
  • We’ll
    • See if we’re already doing it right, using double equals comparison
    • If so, we’re all done, we return.
    def learn(self, perceived, answer):
        if answer == self * perceived:
            return
        

Updating Weights

  • Otherwise, we misfire.
  • Let’s
    • Compute how excited the neuron was.
    • Compare to the bias (take a “delta”)
    def learn(self, perceived, answer):
        if answer == self * perceived:
            return
        delta = self.bias - np.sum(self.weights * perceived)

Updating Weights

  • To determine the update amount, we
    • Divide delta by how many things were perceived.
    • Maybe add a little bit extra, like 10%. to avoid cutting it too close.
    def learn(self, perceived, answer):
        if answer == self * perceived:
            return
        delta = self.bias - np.sum(self.weights * perceived)
        each = 1.1 * delta / sum(perceived)
            

Updating Weights

  • If too much excitement…
    • Fired when weren’t supposed to
    • Reduce the relevant weights by the roughly the delta amount
    • Ignore the other weights.
    • How?

Updating Weights

  • We only get excitement from things we perceive!
    • Multiple each, how much “each” weight should change, by
    • Things that were perceived!
    def learn(self, perceived, answer):
        if answer == self * perceived:
            return
        delta = self.bias - np.sum(self.weights * perceived)
        each = 1.1 * delta / sum(perceived)
        update = each * np.array(perceived)
            

It would look like this.

  • Imagine random weights and an arbitrary bias, say, 3.
is_two = np.random.rand(9)
bias = 3
print(is_two)
[0.55717766 0.39847885 0.24116268 0.65048909 0.73394148 0.97247015
 0.39872539 0.8811111  0.38007921]
  • Does it correctly recognize two?
print(sum(is_two * two) > bias)
False

Well, what’s the delta.

  • Compute delta
delta = bias - sum(is_two * two)
print(delta)
2.3601119333323757
  • How much do we change each weight by?
each = 1.1 * delta / sum(two)
print(each)
1.2980615633328068

What would we update by?

  • Recall, only change where we perceived values!
update = each * np.array(two)
print(update)
[0.         0.         1.29806156 0.         0.         0.
 1.29806156 0.         0.        ]
  • Add these two the weights…
is_two = is_two + update
print(is_two)
[0.55717766 0.39847885 1.53922424 0.65048909 0.73394148 0.97247015
 1.69678695 0.8811111  0.38007921]

Check our work!

  • Does it correctly recognize two?
print(sum(is_two * two) > bias)
True
  • Only one problem!
print(sum(is_two * thr) > bias)
True
  • But, we can just keep updating.

Update for three

  • Compute delta
delta = bias - sum(is_two * thr)
each = 1.1 * delta / sum(thr)
update = each * np.array(thr)
is_two = is_two + update
print(is_two)
[0.55717766 0.39847885 1.18357492 0.65048909 0.37829217 0.97247015
 1.34113764 0.8811111  0.38007921]
  • Check again.
print(sum(is_two * thr) > bias)
False

Updating Weights

  • Let’s add that to our method
    def learn(self, perceived, answer):
        if answer == self * perceived:
            return
        delta = self.bias - np.sum(self.weights * perceived)
        each = 1.1 * delta / sum(perceived)
        update = each * np.array(perceived)
        self.weights = self.weights + update 

Like This

  • Rewrite neuron class to include “learn”
class Neuron:

    def __init__(self, weights=np.random.rand(9), bias=1):
        self.weights = np.array(weights)
        self.bias = bias

    def __repr__(self):
        return "Neuron" + str(self.weights)

    def __mul__(self, other):
        return int(np.sum(self.weights * other) >= self.bias)
        
    def learn(self, perceived, answer):
        if answer == self * perceived:
            return
        delta = self.bias - np.sum(self.weights * perceived)
        each = 1.1 * delta / sum(perceived)
        update = each * np.array(perceived)
        self.weights = self.weights + update 

Make a new neuron

  • Not two exciting.
learner = Neuron()
print(learner)
Neuron[0.34059675 0.3768527  0.33380208 0.86193088 0.59068583 0.95907908
 0.84126294 0.84504323 0.21381956]
  • Let’s teach it to recognize ⚄ and nothing else
for die in dice:
    print(learner * die)
0
1
1
1
1
1

Just have it learn…

  • Instead of just printing what it does…
  • Have it learn in each case.
  • We provide the goal.
goal = [0, 0, 0, 0, 1, 0]

Train steadily…

  • We can loop over all 6 things.
for i in range(6):
    learner.learn(dice[i], goal[i])
  • Easier to see what we’re working with if we print.
for i in range(6):
    print(dice[i], goal[i], learner * dice[i])
[0 0 0 0 1 0 0 0 0] 0 0
[0 0 1 0 0 0 1 0 0] 0 0
[0 0 1 0 1 0 1 0 0] 0 0
[1 0 1 0 0 0 1 0 1] 0 0
[1 0 1 0 1 0 1 0 1] 1 0
[1 0 1 1 0 1 1 0 1] 0 0

Wait!

  • Isn’t that still wrong?
    • Those last two should be equal.
for i in range(6):
    print(dice[i], goal[i], learner * dice[i])
[0 0 0 0 1 0 0 0 0] 0 0
[0 0 1 0 0 0 1 0 0] 0 0
[0 0 1 0 1 0 1 0 0] 0 0
[1 0 1 0 0 0 1 0 1] 0 0
[1 0 1 0 1 0 1 0 1] 1 0
[1 0 1 1 0 1 1 0 1] 0 0
  • The problem:
    • We trained on six last.
    • We changed weights to not recognize six.

Is that so?

  • Retrain five.
learner.learn(fiv, 1)
  • Check it
for i in range(6):
    print(dice[i], goal[i], learner * dice[i])
[0 0 0 0 1 0 0 0 0] 0 0
[0 0 1 0 0 0 1 0 0] 0 0
[0 0 1 0 1 0 1 0 0] 0 0
[1 0 1 0 0 0 1 0 1] 0 0
[1 0 1 0 1 0 1 0 1] 1 1
[1 0 1 1 0 1 1 0 1] 0 1

Oh no!

  • Well now six is wrong again!
    • Let’s see what we have…
print(learner)
Neuron[ 0.18758228  0.3768527  -0.1256639   0.53021633  0.59127919  0.62736452
  0.38179695  0.84504323  0.06080508]
  • And what did I make manually.
print(is_fiv)
Neuron[ 0.2 -0.2  0.2 -0.2  0.2 -0.2  0.2 -0.2  0.2]

The solution

  • What if we just train 100 times.
for _ in range(100):
    r = int(np.random.rand() * 6)
    learner.learn(dice[r], goal[r])
  • Closer?
print(learner)
Neuron[ 1.26543523e-01  3.76852701e-01 -1.86702658e-01  3.21210453e-01
  7.39246313e-01  4.18358648e-01  3.20758197e-01  8.45043230e-01
 -2.33670892e-04]
print(is_fiv)
Neuron[ 0.2 -0.2  0.2 -0.2  0.2 -0.2  0.2 -0.2  0.2]

Stuck in our ways

  • Things didn’t change at all!
    • Training on five means we recognize five and six.
    • Training on six means we don’t recognize anything.
  • We have to reinforce correct guesses!

Recall

  • We just ignored correct classifications!
        if answer == self * perceived:
            temp = perceived + .5
            temp /= sum(perceived)
            self.weights *= temp
  • We want to strengthen correct weights (times 1.5)
  • And weaken incorrect (times .5)
  • But not weight toward six (more dots), so divide.

Like This

  • Rewrite!
class Neuron:

    def __init__(self, weights=np.random.rand(9), bias=1):
        self.weights = np.array(weights)
        self.bias = bias

    def __repr__(self):
        return "Neuron" + str(self.weights)

    def __mul__(self, other):
        return int(np.sum(self.weights * other) >= self.bias)
        
    def learn(self, perceived, answer):
        if answer == self * perceived:
            temp = perceived + .5
            temp /= sum(perceived)
            self.weights *= temp
        else:
            delta = self.bias - np.sum(self.weights * perceived)
            each = delta / sum(perceived)
            update = each * np.array(perceived)
            self.weights = self.weights + update 

Create and train…

  • We can skip a lot of steps now.
learner = Neuron()

for _ in range(1000):
    r = int(np.random.rand() * 6)
    learner.learn(dice[r], goal[r])
  • Same printing we did earlier.
for i in range(6):
    print(dice[i], goal[i], learner * dice[i])
[0 0 0 0 1 0 0 0 0] 0 0
[0 0 1 0 0 0 1 0 0] 0 0
[0 0 1 0 1 0 1 0 0] 0 0
[1 0 1 0 0 0 1 0 1] 0 0
[1 0 1 0 1 0 1 0 1] 1 1
[1 0 1 1 0 1 1 0 1] 0 0

A note

  • I am randomizing this.
    • The above often, but does not always work.
    • To work every time, we have to use calculus.

Fin