You should have a GitHub repo ready to go by the time lab starts. Please let the instructor know if you have any issues accessing it.
There are many good software packages that you can use to experiment with neural networks, such as keras/tensorflow, PyTorch, or aitk. However, before using these packages you should first understand how backpropagation works at a detailed level. Therefore, in this lab you will implement your own neural network from scratch.
Your neural network will be made up of a number of classes, which have been defined in the scaffolding:
As you are working on your implementation, you should frequently try the provided unit tests:
python3 nn_test.pyThere are 18 unit tests in this file. At the very top of the output that is produced are 18 characters summarizing your progress.
Below is a short summary of the tasks you need to complete. Note that much more detail about all of these methods can be found in the comments within the file neural_net.py. Higher-level descriptions and explanations can be found in the lecture notes.
Initially you will focus on implementing the SigmoidNode class methods:
The first three functions store their results in the node's self.activation and self.delta fields. You can run just the individual tests for these 3 methods using: python3 nn_test.py -k test_activation etc.. update_weights changes the weights of the edges stored in self.in_edges.
Next you will work on completing the Network class. It is already set up to initialize a neural network with input, bias, and sigmoid nodes. You must implement the following functions for the neural network:
The main function sets up a network with a single two-neuron hidden layer and trains it to represent the function XOR. You can uncomment the plot_prediction lines to show a visual representation of what the network predicts!
Once you can successfully learn XOR, move on to the next section.
In the latex document provided (called explanation.tex), fill in the diagram of the XOR network with the specific weights that were found by your program. Then explain how your network has solved the XOR problem. In particular, you need to clearly articulate:
It is important to note that this should not just be an exhaustive list of what input will produce what output, but an explanation of how the particular weights that were learned relate to human-understandable concepts such as logic operators.
To compile your latex document into a pdf, run the following command:
pdflatex explanation.texOr if you prefer, a Makefile has also been provided. Once compiled, you can then open the pdf from the command line with the command:
evince explanation.pdf
Note that evince will only work if you are logged in physically; if you are working remotely, you may want to use the PDF-viewer plugin for vscode. You can also set up X-forwarding to get evince to work over an ssh connection.
Be sure to include your name(s) in the title of the document.
Now that we've implemented our own neural network, let's try out some different configurations. We'll use Tensorflow's neural network playground for this. Here's an example of a noisier version of the XOR dataset we've been using: playground . Try pressing play to see the model train! The solution should look a little different than what your network found.
Now, here's a slightly more complicated dataset: nested circles. You should see that the basic network with 2 hidden neurons is no longer enough to separate the two outputs for this dataset. Try adding layers and/or neurons to your network until you are able to train a network that does separate out the two output types. In explanation.tex explain what the simplest network you were able to find that separated the two types looked like.
You should also see that the page linked above has the option to change the learning rate. Try a few different values and take note of what changes in the learning process. Write your observations in explanation.tex. Note that you can also try out your own code for a similar dataset by uncommenting the
Finally take a look at this significantly more complex dataset: interlocking spirals. Once again try to build a network that can perfectly separate these two outputs. You may not be able to do it easily, that's ok! Just get as close as you can and report your best network and observations in explanation.tex. Don't forget to rebuild the pdf when you are finished!
As usual, use git to add, commit, and push your work.