Lab 7: Neural Nets

From 6.034 Wiki

(Difference between revisions)
Jump to: navigation, search
(NeuralNet)
m (NeuralNet)
Line 170: Line 170:
In this lab, inputs are supplied to neural nets in the form of a dictionary that associates each named input with an input value.
In this lab, inputs are supplied to neural nets in the form of a dictionary that associates each named input with an input value.
-
 
-
<b><tt>net</tt></b><tt></tt>
 
You can retrieve the neurons in a network:
You can retrieve the neurons in a network:
Line 189: Line 187:
</ul>
</ul>
-
You can query the parts of the network:
+
Finally, you can query the parts of the network:
<ul>
<ul>
<li><b><tt>net</tt></b><tt>.is_input_neuron(node)</tt>. Return True if the node is connected directly to an input, otherwise False.</li>
<li><b><tt>net</tt></b><tt>.is_input_neuron(node)</tt>. Return True if the node is connected directly to an input, otherwise False.</li>

Revision as of 06:15, 5 November 2015

Contents


This lab is due by TODO at 10:00pm.

To work on this lab, you will need to get the code, much like you did for the first two labs.


Your answers for this lab belong in the main file lab6.py.

Problems

This lab is divided into two independent parts. In the first part, you'll code subroutines necessary for training and using neural networks. In the second part, you'll code subroutines for using and validating support vector machines.

Neural Nets

Wiring a neural net

A neural net is composed of individual neurons, which generally take this form:


Image:Lab6_SimpleNeuron.png


We form the net by combining the neurons into a structure, such as the example shown below.


Image:Lab6_SimpleNeuralNet.png‎


For reference, here are the fundamental equations that define a neural net:

Image:Lab6_NN_Eqns.png


Helper functions

First, you'll code helper functions for the neural nets. The stairstep and sigmoid functions are threshold functions; each neuron in a neural net uses a threshold function to determine whether its input stimulation is large enough for it to emit an output.

stairstep: Computes the output of the stairstep function using the given threshold sigmoid: Computes the output of the sigmoid function using the given steepness and midpoint


The accuracy function is used during training with back-propagation. It measures the performance of the neural net as a function of its actual output (given some set of inputs) and the desired output.

accuracy: Computes accuracy using desired_output and actual_output. If the output is binary, the accuracy ranges from -0.5 to 0

Forward propagation

Forward propagation is the act of running a set of input values through the wired connections in a neural net.

For each neuron in the net, the inputs get multiplied by the weight of the edges they travel along. The weighted inputs are then summed and go through a threshold function. The output of the neuron then continues moving through the net until the final output is computed.

Implement this process, given a neural net, a dictionary of input values, and the provided threshold function in order to compute the binary output of the neural net.


This function should not modify the input net and will return a tuple containing:

1) a dictionary mapping neurons to their immediate outputs

2) the final binary output (0 or 1)

Backward propagation

Backward propagation is the process of updating the weights in our neural net to improve it's accuracy.


A single step of backward propagation involves:

1. Compute output of each neuron using forward propagation and stairstep function

2. Compute δ_B for final layer

3. Compute δ_B for earlier layers

4. Compute updates for weights

5. Update all weights


update_weights: Performs a single step of back propagation. Computes delta_B values and weight updates for entire neural net, then updates all weights. Uses the sigmoid function to compute output.


Returns a tuple containing:

1) the modified neural net, with updated weights

2) a dictionary mapping neurons to delta_B values


back_prop: Updates the weights until the accuracy surpasses minimum_accuracy. Uses the sigmoid function to compute output.


Returns a tuple containing:

1) The modified neural net, with trained weights

2) The number of iterations (that is, the number of weight updates)

Support Vector Machines

Vector Math

norm(v): Returns the length of the vector v. Note that v can either be a Point instance or a tuple/list of coordinates.

dot_product(u,v): Computes the dot product of two vectors u, v. Again, each vector can either be a Point instance or tuple/list of coordinates.

Equations

The following five equations may be helpful. Recall that Equations 1-3 will define the decision boundary and the margin width. Equations 4 & 5 will allow you to calculate the alpha values for the training points.

Image:Lab6 Eqns.png

SVM Functions

positiveness: Evaluates Equation 1 for the given point

classify: Uses the given SVM to classify a Point. We assume that point's classification is unknown. Returns +1 or -1, or 0 if point is on boundary.

margin_width: Calculates the margin width based on current decision boundary.


[I'm changing the docstrings for check_gutter_constraint and check_alphas, so I'll add those later. -jmn) #TODO

Classification Accuracy

[misclassified_training_points -> jmn will add] #TODO

Survey

Please answer these questions at the bottom of your lab6.py file:

  • NAME: What is your name? (string)
  • COLLABORATORS: Other than 6.034 staff, whom did you work with on this lab? (string, or empty string if you worked alone)
  • HOW_MANY_HOURS_THIS_LAB_TOOK: Approximately how many hours did you spend on this lab? (number or string)
  • WHAT_I_FOUND_INTERESTING: Which parts of this lab, if any, did you find interesting? (string)
  • WHAT_I_FOUND_BORING: Which parts of this lab, if any, did you find boring or tedious? (string)
  • (optional) SUGGESTIONS: What specific changes would you recommend, if any, to improve this lab for future years? (string)


(We'd ask which parts you find confusing, but if you're confused you should really ask a TA.)

When you're done, run the online tester to submit your code.

API

Neural Nets

The file neural_net_api.py defines the Wire and NeuralNet classes, described below.

Wire

A Wire is a directed edge that can be used in a neural net to connect an input to a neuron, a neuron to a neuron, or a neuron to OUT.

The class has three attributes - a startNode, an endNode, and a weight.

Note that we have provided a copy method that constructs a new Wire instance with the same parameters to avoid mutability issues. Additionally, you can see if two Wires have the same start,end, and weight using the == equivalence method.

NeuralNet

A NeuralNet is represented as a directed graph. Its fields are:

  • net.inputs, a list of named inputs. Inputs are represented as strings denoting their names.
  • net.neurons, a list of neurons. Neurons are represented as strings denoting their names.
  • net.wires, a list of the wires (edges) which connect the nodes in the network. Each wire is a Wire object (see below.)

In this lab, inputs are supplied to neural nets in the form of a dictionary that associates each named input with an input value.


You can retrieve the neurons in a network:

  • net.get_incoming_neighbors(node). Return a list of the nodes which are connected as inputs to node
  • net.get_outgoing_neighbors(node). Return a list of the nodes to which node sends its output.
  • net.get_output_neuron(). Return the final, output neuron of the network. (In this lab, each neural net has exactly one output neuron.)
  • net.topological_sort(). Return a sorted list of all the neurons in the network. The list is "topologically" sorted, which means that each neuron appears in the list after all the neurons that provide its inputs. Thus, the input layer neurons are first, and the output neuron is last, etc.

You can also retrieve the wires:

  • net.get_wires(startNode=None, endNode=None). Return a list of all the wires in the network. If startNode or endNode are provided, returns only wires that start/end at particular nodes.
  • net.get_incoming_wires(node). Return a list of wires that feed in to the given node.
  • net.get_outgoing_wires(node). Return a list of wires that the node feeds into.

Finally, you can query the parts of the network:

  • net.is_input_neuron(node). Return True if the node is connected directly to an input, otherwise False.
  • net.is_output_neuron(node). Return True if the node is the final, output neuron in the network, otherwise False.
  • net.is_connected(startNode, endNode). Return True if there is a wire from startNode to endNode in the network, otherwise False.




A neural net is represented as a directed graph whose edges are Wires and nodes can be neurons, inputs, or OUT.


Note that:

- Each variable input is represented by its name (a string).

- Each constant input is represented by an int or float (eg -1).

- Each neuron is represented by its name (a string).

- The final output is represented by the constant string NeuralNet.OUT.


The following methods are available for you to use:

get_wires(startNode=None, endNode=None): Returns a list of all the wires in the graph. If the start or end are provided, it restricts to wires that start/end at particular nodes. (A node can be an input, a neuron, or the output OUT.)

get_incoming_neighbors(node): Returns an alphabetical list of neighboring nodes (neurons or inputs) that appear earlier in the neural net. That is, nodes that have wires leading into the provided node. Each node appears at most once.

get_outgoing_neighbors(node): Returns an alphabetical list of neighboring nodes (either be neurons or OUT) that appear later in the neural net (that is, nodes that receive the provided node's output). Each node appears at most once.

get_incoming_wires(endNode): Returns a list of wires leading into the provided neuron or OUT.

get_outgoing_wires(startNode): Returns a list of wires exiting out of the provided neuron or input.

get_wire(startNode, endNode): Returns the wire that directly connects startNode to endNode or None if no such wire exists.

is_connected(startNode, endNode): Returns True if there is a wire that connects startNode to endNode or False if no such wire exists.

is_input_neuron(neuron): Returns True if neuron is an input-layer neuron, else False.

is_output_neuron(neuron): Returns True if neuron is an output-layer neuron, else False.

Support Vector Machines

The file svm_api.py defines the Point, DecisionBoundary, and SupportVectorMachine classes, as well as some helper functions for vector math, all described below.

Point

A Point has a list or tuple of coordinates, and optionally a classification or an alpha value.

We have also provided a copy method to create a new instance of a Point with the same parameters.

Additionally, you can see if two Point objects have equivalent coordinates, classifications, and alpha values using the == equivalence method.


DecisionBoundary

A DecisionBoundary is defined by two parameters: a normal vector w, and an offset b. w is represented as a list or tuple of coordinates.

We have also provided a copy method to create a new instance of a DecisionBoundary with the same parameters.

Additionally, you can see if two DecisionBoundary objects have equivalent w and b values using the == equivalence method.


SupportVectorMachine

A SupportVectorMachine is a classifier that uses a DecisionBoundary to classify points. It has a list of training points and optionally a list of support vectors.

The class has three attributes - the boundary, a list of training_points, and a list of support_vectors.

We have also provided a copy method to create a new instance of a SupportVectorMachine with the same parameters.

You can see if two SupportVectorMachine objects have the same boundary,training points, and support vectors by using the == equivalence method.


Helper functions for vector math

convert_point_to_coords: Given either a Point object or a tuple of coordinates, returns a tuple of coordinates.

vector_add: Given two vectors represented as lists or tuples of coordinates, returns their sum as a list of coordinates.

scalar_mult: Given a constant scalar and a vector (as a tuple or list of coordinates), returns a scaled list of coordinates.

Personal tools