Skip to content

Instantly share code, notes, and snippets.

@sueszli
Created November 11, 2024 18:05
Show Gist options
  • Save sueszli/25091254d109bad1a1e6db0c29e21f3b to your computer and use it in GitHub Desktop.
Save sueszli/25091254d109bad1a1e6db0c29e21f3b to your computer and use it in GitHub Desktop.
dependency free python implementation of mnist
def sigmoid(x): return 1/(1 + __import__('math').exp(-x))
def train_mnist():
# Generate mock MNIST data (28x28 images)
X = [[float(i%2) for i in range(784)] for _ in range(100)] # Mock input
y = [[1 if i==j else 0 for i in range(10)] for j in range(100)] # Mock labels
# Initialize weights and biases
W1 = [[0.01*((i+j)%2) for j in range(784)] for i in range(30)]
W2 = [[0.01*((i+j)%2) for j in range(30)] for i in range(10)]
b1 = [0.0]*30
b2 = [0.0]*10
# Training loop
for epoch in range(5):
for i in range(len(X)):
# Forward pass
h = [sigmoid(sum(W1[j][k]*X[i][k] for k in range(784)) + b1[j]) for j in range(30)]
o = [sigmoid(sum(W2[j][k]*h[k] for k in range(30)) + b2[j]) for j in range(10)]
# Backward pass (simplified)
for j in range(len(W2)):
for k in range(len(W2[0])):
W2[j][k] += 0.1 * (y[i][j] - o[j]) * h[k]
for j in range(len(W1)):
for k in range(len(W1[0])):
W1[j][k] += 0.1 * sum((y[i][m] - o[m]) * W2[m][j] for m in range(10)) * X[i][k]
return W1, W2, b1, b2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment