-SIZES = (rows * cols, 20, 16, 10)
-NUM_LAYERS = len(SIZES)
-
-# initialize weight matrices and bias vectors with random numbers
-weights = []
-biases = []
-for i in range(1, NUM_LAYERS):
- weights.append(rg.normal(size=(SIZES[i], SIZES[i-1])))
- biases.append(rg.normal(scale=10, size=SIZES[i]))
-
-
-def feed_forward(x, transfer=sigmoid):
- '''Compute all z and output vectors for given input vector'''
-
- a_s = [x]
- z_s = []
- for w, b in zip(weights, biases):
- x = w @ x + b
- z_s.append(x)
- a_s.append(transfer(x))
- return (z_s, a_s)
-
-
-def classify(y):
- # the recognized digit is the index of the highest-valued output neuron
- return np.argmax(y), np.max(y)
-
-
-def test():
- """Count percentage of test inputs which are being recognized correctly"""
-
- good = 0
- num_images = test_images.shape[1]
- for i in range(num_images):
- # the recognized digit is the index of the highest-valued output neuron
- y = classify(feed_forward(test_images[:, i])[1][-1])[0]
- good += int(y == test_labels[i])
- return 100 * (good / num_images)
-
-
-res = feed_forward(test_images[:, 0])
-print(f'output vector of first image: {res[1][-1]}')
-digit, conf = classify(res[1][-1])
+# (input)--> [Linear->Sigmoid] -> [Linear->Sigmoid] -->(output)
+# handle 10,000 vectors at a time
+Z1 = nnet.LinearLayer(input_shape=(rows * cols, nnet_batch), n_out=80)
+A1 = nnet.SigmoidLayer(Z1.shape)
+ZO = nnet.LinearLayer(input_shape=A1.shape, n_out=10)
+AO = nnet.SigmoidLayer(ZO.shape)
+net = (Z1, A1, ZO, AO)
+
+res = nnet.forward(net, test_images[:, 0:10000])
+print(f'output vector of first image: {res[:, 0]}')
+digit, conf = nnet.classify(res[:, 0])
+print(f'classification of first image: {digit} with confidence {conf}; real label {test_labels[0]}')
+print(f'correctly recognized images after initialization: {nnet.accuracy(net, test_images, test_labels)}%')
+
+train_y = nnet.label_vectors(train_labels, 10)
+for i in range(100):
+ for batch in range(0, num_train, nnet_batch):
+ cost = nnet.train(net, train_images[:, batch:(batch + nnet_batch)], train_y[:, batch:(batch + nnet_batch)], learning_rate=1)
+ print(f'cost after training round {i}: {cost}')
+print(f'correctly recognized images after training: {nnet.accuracy(net, test_images, test_labels)}%')
+
+res = nnet.forward(net, test_images[:, 0:10000])
+print(f'output vector of first image: {res[:, 0]}')
+digit, conf = nnet.classify(res[:, 0])