0

I need some help getting some clarity on some lingo as it applies to inputs of a perceptron. A function called "update", "Takes in a 2D array @param values consisting of a LIST of inputs and a 1D array @param train, consisting of a corresponding list of expected outputs"

In the code below, what does "values[datapoint]" refer to for the inputs:

np.array([[3,2,1],[4,0,-1]])..."

see full context of this snippet below in code in the last function called "test()", it begins with "p2.updat..." basically, it is calling the function "update()" with this array as part of the inputs.

Does "datapoint" equal "[3,2,1]" or is it referring to just one element of that array like "3"?

import numpy as np

class Perceptron:

    def __init__(self, weights = np.array([1]), threshold = 0):  
        self.weights = weights.astype(float) 
        self.threshold = threshold

    def activate(self, values):       
        strength = np.dot(values,self.weights)       
        return int(strength > self.threshold)

    def update(self, values, train, eta=.1):   
        for data_point in xrange(len(values)):        
           prediction = self.activate(values[data_point])
           error = train[data_point] - prediction
           weight_update = eta * np.dot(values[data_point], error)
           self.weights += weight_update

   def test():
       def sum_almost_equal(array1, array2, tol = 1e-6):
           return sum(abs(array1 - array2)) < tol

       p1 = Perceptron(np.array([1,1,1]),0)
       p1.update(np.array([[2,0,-3]]), np.array([1]))
       assert sum_almost_equal(p1.weights, np.array([1.2, 1, 0.7]))

       p2 = Perceptron(np.array([1,2,3]),0)
       p2.update(np.array([[3,2,1],[4,0,-1]]),np.array([0,0]))
       assert sum_almost_equal(p2.weights, np.array([0.7, 1.8, 2.9]))

       p3 = Perceptron(np.array([3,0,2]),0)
       p3.update(np.array([[2,-2,4],[-1,-3,2],[0,2,1]]),np.array([0,1,0]))
       assert sum_almost_equal(p3.weights, np.array([2.7, -0.3, 1.7]))

   if __name__ == "__main__":
       test()

Thanks.

aL_eX
  • 1,453
  • 2
  • 15
  • 30
spacedustpi
  • 351
  • 5
  • 18

1 Answers1

0

As values is a 2D array say mxn then data_point is a index of rows 1,2,..m. So values[data_point] returns elements in first row i.e [3,2,1]. You can also verify by: In a model, we have to train a model for each single row as a whole and calculate cost function which will be used to update weights. Since, you're using stochastic gradient descent so weight is updated for each input row

sminmgr
  • 312
  • 3
  • 12