Here is how the code appears to break down:
method parameters:
The train(...) method takes an input vector called pattern along with the target output called label and a weight update coefficient (learning rate) called step.
method execution:
train(...) first passes the input vector through our neuron using the predict(...) method.
- The result of
predict(...) is stored in a variable named out_label.
- The
out_label variable is either 1 or -1.
- Depending on the pattern that we provide we want the target output (
label) to be either 1 or -1.
- If
out_label is equal to label (1 == 1 or -1 == -1), then we return True
- Otherwise, the neuron outputted a value we do not want (
-1 != 1 or 1 != -1), so we update the weights accordingly, then return False
example where the neuron is correct:
Let's say we have the following parameters:
input vector (pattern) = [-5,2,8]
target output (label) = 1
The train(...) method executes as follows:
we call the predict(...) which does the following:
passes our vector [-5,2,8] through the neuron using the function np.dot(self.w, pattern) + self.b
stores the result of the above function in a variable called activation (let's say activation = 0.23)
returns 1 because activation > 0
we store the result of predict(...) in the variable out_label, which is 1 in this example
- because our target output (
label = 1), is equal to the actual output (out_label = 1) of the neuron, we return True and consider the neuron output correct.
example where the neuron is incorrect:
Let's say we have the following parameters:
input vector (pattern) = [-1,-5,3]
target output (label) = 1
The train(...) method executes as follows:
we call the predict(...) which does the following:
passes our vector [-1,-5,3] through the neuron using the function np.dot(self.w, pattern) + self.b
stores the result of the above function in a variable called activation (let's say activation = -0.65)
returns -1 because activation <= 0
we store the result of predict(...) in the variable out_label, which is -1 in this example
- because our target output (
label = 1), is not equal to the actual output (out_label = -1) of the neuron, we update the weights using:
self.w += step * (label - out_label) * pattern
self.b += step * (label - out_label)
- we then return
False because the neuron was incorrect