Skip to content

Instantly share code, notes, and snippets.

@KenoLeon
Forked from anshmidt/perceptron_example.py
Created August 2, 2018 22:46
Show Gist options
  • Save KenoLeon/9071827bd2d9c9cfb671c248d4fd458d to your computer and use it in GitHub Desktop.
Save KenoLeon/9071827bd2d9c9cfb671c248d4fd458d to your computer and use it in GitHub Desktop.
Simple example of perceptron
inputs = [0, 1, 0, 0]
weights = [0, 0, 0, 0]
desired_result = 1
learning_rate = 0.2
trials = 6
def evaluate_neural_network(input_array, weight_array):
result = 0
for i in range(len(input_array)):
layer_value = input_array[i] * weight_array[i]
result += layer_value
print("evaluate_neural_network: " + str(result))
print("weights: " + str(weights))
return result
def evaluate_error(desired, actual):
error = desired - actual
print("evaluate_error: " + str(error))
return error
def learn(input_array, weight_array):
print("learning...")
for i in range(len(input_array)):
if input_array[i] > 0:
weight_array[i] += learning_rate
def train(trials):
for i in range(trials):
neural_net_result = evaluate_neural_network(inputs, weights)
learn(inputs, weights)
train(trials)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment