Skip to content

Instantly share code, notes, and snippets.

@anfedorov
Created September 9, 2015 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anfedorov/5a5b3494ccd54af7df9b to your computer and use it in GitHub Desktop.
Save anfedorov/5a5b3494ccd54af7df9b to your computer and use it in GitHub Desktop.
import numpy as np
import struct
def read(f, s, offset=0):
return struct.unpack(f, s[offset:offset+struct.calcsize(f)])
def read_files(imgf, labelf):
f = open(imgf, 'rb')
hf = '>iiii'
_, _, rs, cs = struct.unpack(hf, f.read(struct.calcsize(hf)))
s = f.read()
n = len(s) / struct.calcsize('B') / rs / cs
images = np.ndarray((n, rs*cs, 1), 'B', buffer(s)) / 255.
f = open(labelf, 'rb')
hf = '>ii'
struct.unpack(hf, f.read(struct.calcsize(hf)))
s = f.read()
n = len(s) / struct.calcsize('B')
ls = np.ndarray(n, 'B', buffer(s))
labels = np.zeros((n, 10, 1))
for i, l in enumerate(ls):
labels[i][l][0] = 1.0
# labels = ls
return (images, labels)
training_images, training_labels = read_files('train-images.idx3-ubyte', 'train-labels.idx1-ubyte')
test_images, test_labels = read_files('t10k-images.idx3-ubyte', 't10k-labels.idx1-ubyte')
from ch1 import Network
nn = Network([784, 25, 10])
# from ch1 import sigmoid
# a = test_images[0]
# print sigmoid(np.dot(nn.weights[1], sigmoid(np.dot(nn.weights[0], a) + nn.biases[0])) + nn.biases[1])
nn.SGD(
training_data=zip(training_images, training_labels),
epochs=30,
mini_batch_size=10,
eta=5.0,
test_data=zip(test_images, test_labels)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment