Commit 1fc848fc authored by Davis King's avatar Davis King

Updated the rvm example so that it uses the vector_normalizer

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402560
parent 324148c9
...@@ -73,11 +73,13 @@ int main() ...@@ -73,11 +73,13 @@ int main()
// prevents one large feature from smothering others. Doing this doesn't matter much in this example // prevents one large feature from smothering others. Doing this doesn't matter much in this example
// so I'm just doing this here so you can see an easy way to accomplish this with // so I'm just doing this here so you can see an easy way to accomplish this with
// the library. // the library.
const sample_type m(mean(vector_to_matrix(samples))); // compute a mean vector vector_normalizer<sample_type> normalizer;
const sample_type sd(reciprocal(sqrt(variance(vector_to_matrix(samples))))); // compute a standard deviation vector // let the normalizer learn the mean and standard deviation of the samples
normalizer.train(samples);
// now normalize each sample // now normalize each sample
for (unsigned long i = 0; i < samples.size(); ++i) for (unsigned long i = 0; i < samples.size(); ++i)
samples[i] = pointwise_multiply(samples[i] - m, sd); samples[i] = normalizer(samples[i]);
...@@ -122,73 +124,72 @@ int main() ...@@ -122,73 +124,72 @@ int main()
// value of 0.1 for gamma. The decision function will return values >= 0 for samples it predicts // value of 0.1 for gamma. The decision function will return values >= 0 for samples it predicts
// are in the +1 class and numbers < 0 for samples it predicts to be in the -1 class. // are in the +1 class and numbers < 0 for samples it predicts to be in the -1 class.
trainer.set_kernel(kernel_type(0.1)); trainer.set_kernel(kernel_type(0.1));
decision_function<kernel_type> learned_decision_function = trainer.train(samples, labels); typedef decision_function<kernel_type> dec_funct_type;
typedef normalized_function<dec_funct_type> funct_type;
// Here we are making an instance of the normalized_function object. This object provides a convenient
// way to store the vector normalization information along with the decision function we are
// going to learn.
funct_type learned_function;
learned_function.normalizer = normalizer; // save normalization information
learned_function.function = trainer.train(samples, labels); // perform the actual SVM training and save the results
// print out the number of support vectors in the resulting decision function // print out the number of support vectors in the resulting decision function
cout << "\nnumber of support vectors in our learned_decision_function is " cout << "\nnumber of support vectors in our learned_function is "
<< learned_decision_function.support_vectors.nr() << endl; << learned_function.function.support_vectors.nr() << endl;
// now lets try this decision_function on some samples we haven't seen before // now lets try this decision_function on some samples we haven't seen before
sample_type sample; sample_type sample;
sample(0) = 3.123; sample(0) = 3.123;
sample(1) = 2; sample(1) = 2;
// don't forget that we have to normalize each new sample the same way we did for the training samples. cout << "This sample should be >= 0 and it is classified as a " << learned_function(sample) << endl;
sample = pointwise_multiply(sample-m, sd);
cout << "This sample should be >= 0 and it is classified as a " << learned_decision_function(sample) << endl;
sample(0) = 3.123; sample(0) = 3.123;
sample(1) = 9.3545; sample(1) = 9.3545;
sample = pointwise_multiply(sample-m, sd); cout << "This sample should be >= 0 and it is classified as a " << learned_function(sample) << endl;
cout << "This sample should be >= 0 and it is classified as a " << learned_decision_function(sample) << endl;
sample(0) = 13.123; sample(0) = 13.123;
sample(1) = 9.3545; sample(1) = 9.3545;
sample = pointwise_multiply(sample-m, sd); cout << "This sample should be < 0 and it is classified as a " << learned_function(sample) << endl;
cout << "This sample should be < 0 and it is classified as a " << learned_decision_function(sample) << endl;
sample(0) = 13.123; sample(0) = 13.123;
sample(1) = 0; sample(1) = 0;
sample = pointwise_multiply(sample-m, sd); cout << "This sample should be < 0 and it is classified as a " << learned_function(sample) << endl;
cout << "This sample should be < 0 and it is classified as a " << learned_decision_function(sample) << endl;
// We can also train a decision function that reports a well conditioned probability // We can also train a decision function that reports a well conditioned probability
// instead of just a number > 0 for the +1 class and < 0 for the -1 class. An example // instead of just a number > 0 for the +1 class and < 0 for the -1 class. An example
// of doing that follows: // of doing that follows:
probabilistic_decision_function<kernel_type> learned_probabilistic_decision_function; typedef probabilistic_decision_function<kernel_type> probabilistic_funct_type;
learned_probabilistic_decision_function = train_probabilistic_decision_function(trainer, samples, labels, 3); typedef normalized_function<probabilistic_funct_type> pfunct_type;
pfunct_type learned_pfunct;
learned_pfunct.normalizer = normalizer;
learned_pfunct.function = train_probabilistic_decision_function(trainer, samples, labels, 3);
// Now we have a function that returns the probability that a given sample is of the +1 class. // Now we have a function that returns the probability that a given sample is of the +1 class.
// print out the number of support vectors in the resulting decision function. // print out the number of support vectors in the resulting decision function.
// (it should be the same as in the one above) // (it should be the same as in the one above)
cout << "\nnumber of support vectors in our learned_probabilistic_decision_function is " cout << "\nnumber of support vectors in our learned_pfunct is "
<< learned_probabilistic_decision_function.decision_funct.support_vectors.nr() << endl; << learned_pfunct.function.decision_funct.support_vectors.nr() << endl;
sample(0) = 3.123; sample(0) = 3.123;
sample(1) = 2; sample(1) = 2;
sample = pointwise_multiply(sample-m, sd); cout << "This +1 example should have high probability. It's probability is: " << learned_pfunct(sample) << endl;
cout << "This +1 example should have high probability. It's probability is: "
<< learned_probabilistic_decision_function(sample) << endl;
sample(0) = 3.123; sample(0) = 3.123;
sample(1) = 9.3545; sample(1) = 9.3545;
sample = pointwise_multiply(sample-m, sd); cout << "This +1 example should have high probability. It's probability is: " << learned_pfunct(sample) << endl;
cout << "This +1 example should have high probability. It's probability is: "
<< learned_probabilistic_decision_function(sample) << endl;
sample(0) = 13.123; sample(0) = 13.123;
sample(1) = 9.3545; sample(1) = 9.3545;
sample = pointwise_multiply(sample-m, sd); cout << "This -1 example should have low probability. It's probability is: " << learned_pfunct(sample) << endl;
cout << "This -1 example should have low probability. It's probability is: "
<< learned_probabilistic_decision_function(sample) << endl;
sample(0) = 13.123; sample(0) = 13.123;
sample(1) = 0; sample(1) = 0;
sample = pointwise_multiply(sample-m, sd); cout << "This -1 example should have low probability. It's probability is: " << learned_pfunct(sample) << endl;
cout << "This -1 example should have low probability. It's probability is: "
<< learned_probabilistic_decision_function(sample) << endl;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment