Created
November 11, 2024 18:05
-
-
Save sueszli/25091254d109bad1a1e6db0c29e21f3b to your computer and use it in GitHub Desktop.
dependency free python implementation of mnist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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