1
$\begingroup$

Can someone explain to me how the predict method of the perceptron algorithm works?

def predict(self, pattern):
    activation = np.dot(self.w, pattern) + self.b
    if activation > 0:
        return 1
    else:
        return -1

In this example b stands for bias.

def train(self, pattern, label, step = 0.01):
    out_label = self.predict(pattern)
    if out_label == label:
        return True
    self.w += step * (label - out_label) * pattern
    self.b += step * (label - out_label)
    return False

This is the train method. But as far as I understand in out_label there will only be either 1 or -1. How could this be possibly equal to label? If we would return activation I (think) I would understand it.

$\endgroup$
3
  • $\begingroup$ The weights specify a line, 1 and -1 represents whether the point is on each side of the line. $\endgroup$ May 1 '18 at 15:37
  • $\begingroup$ Okay, I revisited now the entire code. But aren't the weights like 0.something values?How do they represent a line? $\endgroup$
    – ReRed
    May 1 '18 at 16:22
  • $\begingroup$ Take a look at here. About the weights, do you know about decision boundary? I guess the link may help you. $\endgroup$ May 1 '18 at 18:31
0
$\begingroup$

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:

  1. train(...) first passes the input vector through our neuron using the predict(...) method.
  2. The result of predict(...) is stored in a variable named out_label.
  3. The out_label variable is either 1 or -1.
  4. Depending on the pattern that we provide we want the target output (label) to be either 1 or -1.
  5. If out_label is equal to label (1 == 1 or -1 == -1), then we return True
  6. 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:

  1. 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

  2. we store the result of predict(...) in the variable out_label, which is 1 in this example

  3. 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:

  1. 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

  2. we store the result of predict(...) in the variable out_label, which is -1 in this example

  3. 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)
  4. we then return False because the neuron was incorrect
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.