








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions for downloading and installing the orange software package for a machine learning lab. It also explains the implementation of missing functions for a neural network api, specifically the doutdx() function for input, performanceelem, and neuron classes. The document also mentions the use of recursion and the chain rule in calculating derivatives.
Typology: Exercises
1 / 14
This page cannot be seen from the preview
Don't miss anything!









This is the last problem set in 6.034!
To work on this problem set, you will need to get the code.
python orange_for_6034.py
and you should get a version string and no errors.
To work on this lab on *Athena you'll need to:
*Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
export LD_LIBRARY_PATH=/afs/athena.mit.edu/course/6/6.034/lib/python2.5/dist- packages/orange:$LD_LIBRARY_PATH
to your .bashrc.mine file. If you are using tcsh or csh run:
setenv LD_LIBRARY_PATH /afs/athena.mit.edu/course/6/6.034/lib/python2.5/dist- packages/orange:$LD_LIBRARY_PATH
instead. You may omit :$LD_LIBRARY_PATH if you get complaints about it not being set. The above settings tell python where to look for shared libraries required for running Orange. NOTE: If you don't know what the shell you are running, type "echo $SHELL".
Your answers for the problem set belong in the main file lab5.py, as well as neural_net.py and boost.py.
In this part of Lab 5, you are to complete the API for a Neural Net. Then afterwards, you are to construct various neural nets using the API to solve abstract learning problems.
We have provided you a skeleton of the Neural Net in neural_net.py, you are to complete the unimplemented methods.
The three classes, Input, PerformanceElem, and Neuron all have incomplete implementation of the following two functions:
def output(self) def dOutDX(self,elem)
output(self) produces the output of each of these elements.
dOutDx(self, elem) generates the value of the partial derivative, given a weight element. Your first task is to fill in all 6 functions to complete the API.
*Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
o = s(z) = 1.0 / (1.0 + e**(-z)) ds(o)/dz = s(z) * (1 - s(z))
The performance function and its derivative as discussed in class
P(o) = -0.5 (d - o)** dP(o)/dx = (d - o)
Most of the classes in the Neural Network inherit from the following two abstract classes:
ValuedElement
This interface class allows an element to have a settable value. Input (e.g. i1, i2) and Weight (e.g. w1A, wAB) are subclasses of ValueElement
Elements that are subclassed all have these methods:
DifferentiableElement
This abstract class defines the interface for elements that have outputs and are involved in partial derivatives.
Inputs, Neurons, and PerformanceElem are the three subclasses that implement DifferentiableElements. You will have to complete the interface for these classes.
Weight(ValuedElement)
Represents update-able weights in the network. It has in addition to ValueElement functions:
Both methods are used for the training algorithm itself and you will not need them in your implementation.
Input(DifferentiableElement, ValuedElement)
Represents inputs to the network. These may represent variable inputs as well as fixed inputs (i.e. threshold inputs) that are always set to -1. output() of Input units should simply return the value they are set to during training or testing.
dOutdX(self, elem) of an Input unit should return 0, since there are no weights connect directly into inputs.
Neuron(DifferentiableElement)
Represents the actual neurons in the neural net. The definitions for output and dOutdX already contains some code. Namely, we've implemented a value caching mechanism to speed up the training / testing process. So instead of implementing output and dOutdx directly you should instead implement:
PerformanceElem(DifferentiableElement)
Represents a Performance Element that allows you to set the desired output.
The desired output is set during training. You will not need it for your implementation.
To better understand back-propagation, you should take a look at the methods train and test in neural_network.py to see how everything is put together.
Once you've completed the missing functions, we have provided a python script neural_net_tester.py to help you unit test. You will need to pass the tests in this unit tester before you can move on to the next parts.
To check if your implementation works, run:
python neural_net_tester.py simple
This makes sure that your code works and can learn basic functions such as AND and OR.
Once you have finished implementing the Neural Net API, you will be tasked to build three networks to learn various abstract data sets.
Here is an example of how to to construct a basic neural network:
Now use the Neural Net API you've just completed and tested to create a two layer network that looks like the following.
Fill your answer in the function stub: def make_neural_net_two_layer in neural_net.py.
Your 2-layer neural net should now be able to learn slightly harder datasets, such as the classic non-linearly separable examples such as NOT-EQUAL (XOR), and EQUAL.
When initializing the weights of the network, you should use random weights. To get deterministic random initial weights so that tests are reproducible you should first seed the random number generator, and then generate the random weights.
seed_random()
wt = random_weight() ...use wt... wt2 = random_weight() ...use wt2...
Note: the function random_weight() in neural_net.py uses the python function random.randrange(-1,1) to compute initial weights. This function generates values: -1, 0, 1 (randomly). While this may seem like a mistake but what we've found empirically is that this actually performs better than using random.uniform(-1, 1). Be our guest and play around with the
random_weight function. You'll find that Neural Nets can be quite sensitive to initialization weight settings. Recall what happens if you set all weights to the same value?
To test your completed network run:
python neural_net_tester.py two_layer
Your network should learn and classify all the datasets in the neural_net_data.harder_data_set with 100% accuracy.
Now it's your turn to design the network. We want to be able to classify more complex data sets.
Specifically we want you to design a new network that should theoretically be able to learn and classify the following datasets:
4 + - 3 + - 2 + - 1 + - - - - 0 - + + + + 0 1 2 3 4
4 - - - - - 3 - - 2 - + - 1 - - 0 - - - - - 0 1 2 3 4
4 - - + + 3 - - + + 2 1 + + - - 0 + + - - 0 1 2 3 4
We claim that a network architecture containing 5 neuron nodes or less can fully learn and classify all three shapes. In fact we required it!
Construct a new network in:
def make_neural_net_challenging
You're still trying to use AI to predict the votes of politicians. ID-Trees were great, but you've heard about these other magnificent learning algorithms like SVMs and Boosting. Boosting sounds easier to implement and had a pretty good reputation, so you decide to start with that.
To make sure that you interpret the results without letting your political preconceptions get in the way, you dig up some old data to work with: in particular, the data from the 4th House of Representatives, which met from 1796 to 1797. (According to the records on voteview.com, this is when the two-party system first emerged, with the two parties being designated "Federalists" and "Republicans".)
You experiment with that data before going on to the 2007-2008 data, finding that Congressmen in 1796 were much more clear about what they were voting on than in 2008.
The framework for a boosting classifier can be found in boost.py. You need to finish coding it, and then use it to learn some classifiers and answer a few questions.
The following resources will be helpful:
The boosting code uses a trick that means it only has to try half the number of base classifiers.
It turns out that AdaBoost does not really care which side of its base classifier is +1 and which side is -1. If you choose a classifier that is the opposite of the best classifier -- it returns -1 for most points that should be +1, and returns +1 for most points that should be -1, and therefore has a high error rate -- it works the same as if you had chosen the negation of that classifier, which is the best classifier.
If the data reweighting step is implemented correctly, it will produce the same weights given a classifier or its opposite. Also, a classifier with a high error rate will end up with a negative alpha value, so that in the final "vote" of classifiers it will act like its opposite. So the important thing about a classifier isn't that its error rate is low -- it's that the error rate is far from 1/.
In the boosting code, we take advantage of this. We include only classifiers that output +1 for voting YES on a particular motion, or +1 for voting NO on a particular motion, and as the "best classifier" we choose the classifier whose error rate is farthest from 1/2. If the error rate is high, then the result will act like a classifier that outputs +1 for "voting NO or abstaining", or +1 for
"voting YES or abstaining", respectively. This means we don't have to include these classifiers in the base classifier list, speeding up the algorithm by a factor of 2.
Here are the parts that you need to complete:
Remember to use the supplied legislator_info(datum) to output your list of the most- misclassified data points!
Answer the two questions republican_newspaper_vote and republican_sunset_vote in lab5.py.
When you are asked how a particular political party would vote on a particular motion, disregard the possibility of abstaining. If your classifier results indicate that the party wouldn't vote NO, consider that an indication that the party would vote YES.
First things first: Have you installed Orange yet?
Now that you've installed Orange, when you run lab5.py, does it complain about Orange, or does it show you the outputs of some classifiers on vampire data?
This part is to get you familiar with the Orange GUI to do a little machine learning without doing any programming. We've given you some data files (vampires.tab, H004.tab, adult.tab, titanic.tab, breast-cancer.tab, etc.) that you can play with. Try making something like the following:
successively tests by hiding each fold in turn, and averages over the results. You will find with cross-validation that the classifiers do much better on identifying political parties than they do on vampires. That's because the vampire dataset has so few examples, with so little redundancy, that if you take away one example, it is very likely to remove the information you actually need in order to classify that example.
To ensure that you've gotten the right idea from each part of describe_and_classify, there are six questions just below the function.
You may be able to do better by using AdaBoost over your Orange classifiers than you could do with any of those Orange classifiers by themselves. Then again, you may do worse. That AdaBoost "doesn't tend to overfit" is a somewhat conditional statement that depends a lot on the particular classifiers that boosting gets its pick of. If some of the underlying classifiers tend to overfit, then boosting will greedily choose them first, and it will be stuck with those choices for testing.
We have set up a learner that uses the BoostClassifier from the first part, but its underlying classifiers are the Orange classifiers we were just looking at. When you combine classifiers in this way, you create what's called an "ensemble classifier". You will notice, as you run this new classifier on the various data sets we've provided, that the ensemble frequently performs worse in cross-validation than some (or most) of its underlying classifiers.
Your job is to find a set of classifiers for the ensemble that get at least 74% accuracy on the breast-cancer dataset. You may use any subset of the classifiers we've provided. Put the short names of your classifiers into the list classifiers_for_best_ensemble. There will be honorable mention in class for the best ensemble. Classifier performance appears to be architecture dependent, so you might be able to get to 74% with just one classifier on your machine, but that won't be enough on the server -- in this case, try to get even better performance at home. If you are proud of the way that you went about choosing the best ensemble, let us know to look at your code carefully, and there may be another honorable mention for that.
We think there may be a problem with the code somewhere, but we don't know quite where. If you run the ensemble boosting with the original set of learners (one of each) then you get terrible results for the H109 data set. Can you figure out why?
Neural Nets
If you are having problems with getting your network to convergence on certain problems, try the following:
Boosting and Orange
It turns out that different architectures yield different results from describe_and_classify on the vampire and H004 datasets. Some folks have perfectly reasonable answers to the short answer part given the output on their machine, but do not pass the tests with those answers.
For the same reason, some folks are getting 74% on their home computers very easily. The question was intended to set a lower limit which was larger than any of the classifiers by itself. If you're getting 74, but the ensemble classifier test is failing, you'll need to try harder. You'll want to use some subset of the classifiers. Think about why the decision tree isn't a good choice as a base classifier for boosting.