Hands On: Time Travel Testing

You might be itching to see the neural network running, even though it’s incomplete. Right now we have the classification code, that needs pre-calculated weights—but not the training code, that finds the values of those weights. How can we check the first without the second?

We can solve this problem thanks to time travel. Book authors have mastered that elusive skill—and guess what: I just returned from the future, where I have access to the complete neural network code. After training the complete network for a few iterations, I serialized the two matrices of weights to a JSON file named weights.json, that you can find among this chapter’s source code.

Go forth, give it a try. Paste this code at the end of forward_propagation.py and run it:

 import​ ​json
 with​ open(​'weights.json'​) ​as​ f:
  weights = json.load(f)
 w1, w2 = (np.array(weights[0]), np.array(weights[1]))
 
 import​ ​mnist
 report(0, mnist.X_train, mnist.Y_train, mnist.X_test, mnist.Y_test, w1, w2)

This code uses Python’s json.load function to deserialize w1 and w2 from the weights.json file. Then it runs a classification on the MNIST test set. If you try it yourself, you should get a bit more than 43% correct matches.

That 43% is nothing to write home about—but remember, these are the weights that I got after just a handful of training iterations. To see how good the neural network gets after some serious training, you’ll have to wait for the next chapter. How’s that for a cliffhanger?

Footnotes

[15]

https://www.progml.com

[16]

https://arxiv.org/abs/1412.0233