|
|
|
|
|
by SubiculumCode
3 days ago
|
|
I wish that the tutorial went just one more step. It presents a one dimensional perceptron. But most perceptrons are multi-input.
Adapting the article's 1D perceptron to three-input, for example: import random
learning_rate = 0.1
EPOCHS = 50
NUM_INPUTS = 3
weights = [random.uniform(-1, 1) for _ in range(NUM_INPUTS)]
bias = random.uniform(-1, 1)
data = []
for _ in range(100):
inputs = [random.uniform(-1, 1) for _ in range(NUM_INPUTS)]
result = sum(inputs) > 0
data.append((inputs, result))
for epoch in range(EPOCHS):
for inputs, result in data:
weighted_sum = bias
for i in range(NUM_INPUTS):
weighted_sum += inputs[i] * weights[i]
prediction = weighted_sum > 0
if prediction != result:
error = int(result) - int(prediction)
for i in range(NUM_INPUTS):
weights[i] += learning_rate * error * inputs[i]
bias += learning_rate * error
print(f"Final weights: {[round(w, 3) for w in weights]}")
print(f"Final bias: {round(bias, 3)}")
|
|