PacktPublishing / Hands-On-Deep-Learning-for-Games

MIT License
38 stars 17 forks source link

Chapter_1_1.py #5

Open ngkangtze opened 2 years ago

ngkangtze commented 2 years ago
inputs = [1,2]
weights = [1,1,1]

def perceptron_predict(inputs, weights):
    activation = weights[0]    
    for i in range(len(inputs)-1):
      activation += weights[i+1] * inputs[i]
    # Added missing indentation
    return 1.0 if activation >= 0.0 else 0.0

print(perceptron_predict(inputs,weights))
ArjunVarma39 commented 2 years ago

Hello @ngkangtze

Could you please explain the problem with the code?

ngkangtze commented 2 years ago
# original code in book
inputs = [1,2]
weights = [1,1,1]

def perceptron_predict(inputs, weights):
    activation = weights[0]    
    for i in range(len(inputs)-1):
      activation += weights[i+1] * inputs[i]
      return 1.0 if activation >= 0.0 else 0.0

print(perceptron_predict(inputs,weights))

In the original code, you return after 1 iteration, which should not be the case. I shifted the indentation of the return line so that it only returns after iterating through all weights.