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
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.
Compare
- Find out which die we have in a single line using vector arithmetic.
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)
fivbelongs to. - We would start with
fiv
- We would want to get back something with a
1in the fiveth position, and zeroes elsewhere.
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.
- Only the neuron representing “five” will fire!
Step Back
Collect Our Thoughts
- Is this intelligent:
[[-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.
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
0or1or perhapsTrueorFalseetc.
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
Edges
- Edges in this graph could connect sensed dots to numeric meaning.
- Middle square to odd numbers.
Weights
- Some edges may matter more than others.
- Center is very important for
1 - Kinda important for
3and5- where corners matter too.
- Center is very important for
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.
Lastly
- Then, we noted we could express a neuron as an object.
Example
- We can make, say, the neuron that recognizes ⚄
Try it
- We can verify the neuron “recognizes” ⚄
- But nothing else…
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:
- 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
- Start with “nothing”
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.
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!
[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.randand tell it what shape we want.
View it
- It looks like this:
- All between
0and1by default.
- All between
[[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:
- 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
Check it out!
- 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
.1or something.
- Perhaps increase them by 10% or by
- If the prediction is incorrect, we can “penalize” the connections.
- Perhaps decrease them by 10% or by
.1or something.
- Perhaps decrease them by 10% or by
“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
selfsince it’s a function inside of a neuron.
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.
- We’ll get there!
Using dunder methods
- It is possible to use dunder methods the same way, it is just extremely ugly.
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
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
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.
Updating Weights
- This isn’t too bad.
- We’ll
- See if we’re already doing it right, using double equals comparison
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.
Updating Weights
- Otherwise, we misfire.
- Let’s
- Compute how excited the neuron was.
- Compare to the bias (take a “delta”)
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.
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!
- Multiple
It would look like this.
- Imagine random weights and an arbitrary bias, say,
3.
[0.55717766 0.39847885 0.24116268 0.65048909 0.73394148 0.97247015
0.39872539 0.8811111 0.38007921]
- Does it correctly recognize two?
Well, what’s the delta.
- Compute delta
- How much do we change each weight by?
What would we update by?
- Recall, only change where we perceived values!
- Add these two the weights…
Check our work!
- Does it correctly recognize two?
- Only one problem!
- 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.
Updating Weights
- Let’s add that to our method
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.
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
Just have it learn…
- Instead of just printing what it does…
- Have it learn in each case.
- We provide the goal.
Train steadily…
- We can loop over all 6 things.
- Easier to see what we’re working with if we print.
Wait!
- Isn’t that still wrong?
- Those last two should be equal.
[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.
- Check it
Oh no!
- Well now six is wrong again!
- Let’s see what we have…
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.
The solution
- What if we just train 100 times.
- Closer?
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]
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!
- 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.
- Same printing we did earlier.
A note
- I am randomizing this.
- The above often, but does not always work.
- To work every time, we have to use calculus.
