runner16
runner16

Reputation: 107

Perceptron Algorithm Confusion

I have this problem that I'm trying to solve:

def phi(x):
    # DO NOTHING ON THIS FUNCTION
    if x<=0:
        return -1.0
    else:
        return 1.0
    
phi = np.vectorize(phi)

This is where I need to implement the function:

def predictOne(x, w):
    z =  
    return phi(z)

But when I try to use my formula but with my input code:

def predictOne(x, w):
    z =  0 + x * w
    return phi(z)

And run my asserts:

assert predictOne(np.array([0.0,0.0]) , np.array([0.1,3.2,7.4])) == 1.0
assert predictOne(np.array([0.0,0.0]), np.array([-0.1,3.2,7.4])) == -1.0
assert predictOne(np.array([0.3,-0.7]), np.array([0.1,3.2,7.4])) == -1.0
assert predictOne(np.array([0.3,0.7]), np.array([0.1,3.2,7.4])) == 1.0

I get an (operands could not be broadcast together with shapes (2,) (3,)

Apparently, the assertions are correct so I'm doing somethin wrong in my predictOne function. Can anyone help?

Upvotes: 0

Views: 106

Answers (1)

Marat
Marat

Reputation: 15738

The problem is in the formula:

def predictOne(x, w):
    z = w[0] + np.sum(x * w[1:])
    return phi(z)
  • w[0] is bias. Bias of zero, as pointed out by @mkrieger, doesn't make a lot of sense.
  • perceptron essentially calculates weighted sum of inputs, so the result should be summed up.

Upvotes: 1

Related Questions