Week 6 Lab: Inference with Neural Networks

Overview: In this lab you will be building pieces of your own neural network to perform inference with an OR logic gate as well as a pre-trained tic-tac-toe board. You are not allowed to use any neural network or machine learning libraries to implement this, and you are expected to use vectorization with NumPy. You are given a stubbed-out neural network implementation in Python that you will be updating.

You are encouraged to watch the following video to supplement your understanding of neural networks: https://www.youtube.com/watch?v=aircAruvnKk

Learning outcomes for this lab:

Instructions

You are given a basic neural network implementation, nn.py This serves as your starting point to implement this week’s lab. This network has hardcoded weights and biases for a 4-node OR logic gate network. It also contains pre-trained, hardcoded weight and bias values that can be loaded for the data.

Start by implementing the inference method (and any other methods it needs). Please follow the notation and descriptions from class to ensure your code is easy to debug. Use Numpy in your solution; you should be able to write it in about five lines of code with no loops. Check the shapes of the arrays to confirm they are as you have assumed.

Note the places where changes are needed are marked by FIXME. Work on all of the items marked “week 6”.

You are also given driver code: nn_driver.py. This sets up the input data, output labels, and runs a feedforward pass for both the OR network as well as the tic-tac-toe network. You will need to implement the file import for the tic-tac-toe data as well as the accuracy and precision calculation method. You should write some code to use your methods to determine the accuracy of both the OR network and the tic-tac-toe network. Because we are using the sigmoid function, it is reasonable to round the output for determining accuracy and precision. If you are not sure you are computing them correctly, try different thresholds and confirm they have the expected effect on the computed accuracy and precision.

The input file tic-tac-toe.csv contains training data for your neural network. Each row of this file contains a final board configuration including whether X wins or the game has no winner (“a cat’s game”). The no winner case is marked with “CWin”. Note this file does not contain all possible configurations; the goal is to classify just a subset. The format of the file is that columns 1–9 correspond to the position on the tic-tac-toe board below. The values in xthe column represent whether an “X” or an “O” occupies the space. The last column represents whether X wins (Xwin) or it is a cat’s game (no one wins, Cwin). There are no “OWin” cases since they are just mirrors of XWin cases.

You should use the provided data for testing the OR network with the OR data and AND data (embedded in the nn_driver.py file) and report the accuracy and precision your calculated for these two experiments. Additionally, you should report the accuracy and precision of the pre-trained tic-tac-toe data. Do not attempt to use libraries to compute accuracy and precision; these are simple to compute with standard code. Accuracy is the number of correct predictions divided by the total number of predictions (see Wikipedia) and precision is the number of true positives divided by the sum of true positives and false positives (Section 7.2.2 of the textbook).

Submission

Submit the following:

Critical: be sure your screen capture gives your instructor the information they need to see how your program runs and its result. We need to see the command you type, the information you enter, and all output. If you need to abbreviate the output, be sure to document how and add a note explaining the missing bits. When capturing this, it must be readable. A full screen capture means the text you want your instructor to read is very small (relative sizes!). When you are done, look at your results in your document. If you have to squint to see detail, no one else can read it either!

Be sure your accuracy and precision are obvious, circling them if necessary.

Any code you write must conform to your instructor’s standards.

You will upload your .py files and a .pdf of your document. Creating a readable document is an important part of this lab!