Skip to content

Instantly share code, notes, and snippets.

@TupotaValentyn
Last active January 14, 2024 23:38
Show Gist options
  • Save TupotaValentyn/9a8c8e80c899ee53f97b05451a33764f to your computer and use it in GitHub Desktop.
Save TupotaValentyn/9a8c8e80c899ee53f97b05451a33764f to your computer and use it in GitHub Desktop.
Neural Network sample 1
count = 10
coefficient = 2
inputs = [n + 1 for n in range(count)]
targets = [i * coefficient for i in inputs]
EPOACH = 25
w = 0.1
learning_rate = 0.1
def predict(i):
return w * i
for _ in range(EPOACH):
pred = [predict(i) for i in inputs]
errors = [t - p for p, t in zip(pred, targets)]
cost = sum(errors) / len(targets)
print(f"Weight: {w:.2f}, Cost: {cost:.2f}")
w += learning_rate * cost
# Network Tests
test_inputs = [5, 6, 7, 8, 9]
test_targets = [10, 12, 14, 16, 18]
print(f"W: {w:.2f}")
pred = [predict(i) for i in test_inputs]
for i, t, p in zip(test_inputs, test_targets, pred):
print(f"inputs: {i}, targets: {t}, pred: {p:.4f}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment