Skip to content

Instantly share code, notes, and snippets.

@fielding
Last active December 11, 2024 22:35
Show Gist options
  • Save fielding/338bfaf3e90781c80c18c1f9f0435cf2 to your computer and use it in GitHub Desktop.
Save fielding/338bfaf3e90781c80c18c1f9f0435cf2 to your computer and use it in GitHub Desktop.
machine learning in a nutshell
import random
# let's fuck around!
weights = [random.uniform(-1, 1) for _ in range(3)]
# "out" (ground truth we want to find)
target = [ord(c) for c in "out"] # ASCII values of 'o', 'u', 't'
# rate of fucking around
learning_rate = 0.1
def loss_fn(output, target):
return sum((o - t) ** 2 for o, t in zip(output, target))
def forward(weights):
# Map to printable ASCII: [32, 126]
return [32 + round(w * 100) % 95 for w in weights]
def backward(weights, output, target):
# Internalizing a more proficient fuck-around strategy
gradients = [(o - t) / 100 for o, t in zip(output, target)]
return [w - learning_rate * g for w, g in zip(weights, gradients)]
for epoch in range(1000):
output = forward(weights)
loss = loss_fn(output, target)
print(f"{epoch}: fucked around with {output} -> loss = {loss:.2f}")
# Check if we "found 'out'"
if output == target:
print(f"Found \"out\"! Weights = {weights}")
break
weights = backward(weights, output, target)
print("Fucked around and found \"out\": ", weights)
0: fucked around with [72, 69, 110] -> loss = 3861.00
1: fucked around with [76, 74, 111] -> loss = 3099.00
2: fucked around with [79, 79, 112] -> loss = 2484.00
3: fucked around with [83, 82, 112] -> loss = 2025.00
4: fucked around with [85, 86, 112] -> loss = 1653.00
5: fucked around with [88, 89, 113] -> loss = 1322.00
6: fucked around with [90, 92, 113] -> loss = 1075.00
7: fucked around with [92, 94, 113] -> loss = 899.00
8: fucked around with [94, 97, 114] -> loss = 693.00
9: fucked around with [96, 99, 114] -> loss = 553.00
10: fucked around with [97, 100, 114] -> loss = 489.00
11: fucked around with [99, 102, 114] -> loss = 373.00
12: fucked around with [100, 104, 114] -> loss = 294.00
13: fucked around with [101, 105, 115] -> loss = 245.00
14: fucked around with [102, 106, 115] -> loss = 203.00
15: fucked around with [103, 107, 115] -> loss = 165.00
16: fucked around with [104, 108, 115] -> loss = 131.00
17: fucked around with [105, 109, 115] -> loss = 101.00
18: fucked around with [105, 110, 115] -> loss = 86.00
19: fucked around with [106, 111, 115] -> loss = 62.00
20: fucked around with [106, 111, 115] -> loss = 62.00
21: fucked around with [107, 112, 115] -> loss = 42.00
22: fucked around with [107, 112, 116] -> loss = 41.00
23: fucked around with [108, 113, 116] -> loss = 25.00
24: fucked around with [108, 113, 116] -> loss = 25.00
25: fucked around with [108, 114, 116] -> loss = 18.00
26: fucked around with [108, 114, 116] -> loss = 18.00
27: fucked around with [109, 114, 116] -> loss = 13.00
28: fucked around with [109, 114, 116] -> loss = 13.00
29: fucked around with [109, 115, 116] -> loss = 8.00
30: fucked around with [109, 115, 116] -> loss = 8.00
31: fucked around with [110, 115, 116] -> loss = 5.00
32: fucked around with [110, 115, 116] -> loss = 5.00
33: fucked around with [110, 116, 116] -> loss = 2.00
34: fucked around with [110, 116, 116] -> loss = 2.00
35: fucked around with [110, 116, 116] -> loss = 2.00
36: fucked around with [110, 116, 116] -> loss = 2.00
37: fucked around with [110, 116, 116] -> loss = 2.00
38: fucked around with [110, 116, 116] -> loss = 2.00
39: fucked around with [110, 116, 116] -> loss = 2.00
40: fucked around with [110, 116, 116] -> loss = 2.00
41: fucked around with [111, 116, 116] -> loss = 1.00
42: fucked around with [111, 116, 116] -> loss = 1.00
43: fucked around with [111, 117, 116] -> loss = 0.00
Found "out"! Weights = [-0.16425843423790087, -0.10411822438953233, -0.11456350997178]
Fucked around and found "out": [-0.16425843423790087, -0.10411822438953233, -0.11456350997178]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment