Commit 445d6a75 authored by Rakesh MEHTA's avatar Rakesh MEHTA
Browse files

scores are added along with the labels for return

parent 3ecc3a08
No preview for this file type
......@@ -159,9 +159,9 @@ class Boost:
Shape for multivariate classification: #number of samples x #number of outputs
Examples for 4 classes case (0,1,2,3) and three test samples.
[[0,1,0,0], #Predicted class is 1
[1,0,0,0], #Predicted class is 0
[0,0,0,1]] #Predicted class is 3
[[-1, 1, -1, -1], #Predicted class is 1
[ 1, -1, -1, -1], #Predicted class is 0
[-1, -1, -1, 1]] #Predicted class is 3
There can be only single 1 in a row and the index of 1 indicates the class.
Return:
......@@ -198,7 +198,7 @@ class Boost:
for r in range(self.num_rnds):
# Compute the gradient of the loss function, l'(y,f(x)) using loss_ class
# Compute the gradient of the loss function, l'(y,f(x)) using loss_class
loss_grad = loss_func.update_loss_grad(targets,pred_scores)
# Select the best weak trainer for current round of boosting
......@@ -217,7 +217,7 @@ class Boost:
# Update the prediction score after adding the score from the current weak classifier f(x) = f(x) + alpha_r*g_r
pred_scores = pred_scores + alpha* curr_pred_scores
pred_scores = pred_scores + alpha*curr_pred_scores
# Add the current trainer into the boosting machine
......@@ -279,10 +279,10 @@ class BoostMachine():
Shape for multivariate classification: #number of samples x #number of outputs
Examples for 4 classes case (0,1,2,3) and three test samples.
[[0,1,0,0], #Predicted class is 1
[1,0,0,0], #Predicted class is 0
[0,0,0,1]] #Predicted class is 3
There can be only single 1 in a row and the index of 1 indicates the class.
[[-1, 1, -1, -1], #Predicted class is 1
[ 1, -1, -1, -1], #Predicted class is 0
[-1, -1, -1, 1]] #Predicted class is 3
There can be only single 1 in a row and the index of 1 indicates the class.
"""
# Initialization
......
......@@ -72,7 +72,7 @@ def main(argv):
model = boost_trainer.train(fea_train, label_train)
# Classify the test samples (testsamp) using the boosited classifier generated above
prediction_labels = model.classify(fea_test)
pred_scores, prediction_labels = model.classify(fea_test)
# calculate the accuracy in percentage for the curent classificaiton test
label_test = label_test[:,numpy.newaxis]
......
......@@ -70,7 +70,7 @@ def main():
machine = boost_trainer.train(fea_train, label_train)
# Classify the test samples (testsamp) using the boosited classifier generated above
prediction_labels = machine.classify(fea_test)
pred_scores, prediction_labels = machine.classify(fea_test)
# calculate the accuracy in percentage for the curent classificaiton test
# label_test = label_test[:,numpy.newaxis]
......
......@@ -70,7 +70,7 @@ def main():
machine = boost_trainer.train(fea_train, label_train)
# Classify the test samples (testsamp) using the boosited classifier generated above
prediction_labels = machine.classify(fea_test)
pred_scores, prediction_labels = machine.classify(fea_test)
# calculate the accuracy in percentage for the curent classificaiton test
#label_test = label_test[:,numpy.newaxis]
......
......@@ -70,7 +70,7 @@ def main():
model = boost_trainer.train(fea_train, label_train)
# Classify the test samples (testsamp) using the boosited classifier generated above
prediction_labels = model.classify(fea_test)
pred_scores, prediction_labels = model.classify(fea_test)
# calculate the accuracy in percentage for the curent classificaiton test
label_test = label_test[:,numpy.newaxis]
......
......@@ -88,7 +88,7 @@ def main():
machine = boost_trainer.train(train_fea, label_train)
# Classify the test samples (testsamp) using the boosited classifier generated above
prediction_labels = machine.classify(test_fea)
pred_scores, prediction_labels = machine.classify(test_fea)
# calculate the accuracy in percentage for the curent classificaiton test
label_test = label_test[:,numpy.newaxis]
......
......@@ -71,7 +71,7 @@ def main():
machine = boost_trainer.train(fea_train, train_targets)
# Classify the test samples (testsamp) using the boosited classifier generated above
prediction_labels = machine.classify(fea_test)
pred_scores, prediction_labels = machine.classify(fea_test)
# Calulate the values for confusion matrix
confusion_matrix = numpy.zeros([num_digits,num_digits])
......
......@@ -90,7 +90,7 @@ def main():
machine = boost_trainer.train(train_fea, train_targets)
# Classify the test samples (testsamp) using the boosited classifier generated above
prediction_labels = machine.classify(test_fea)
pred_scores, prediction_labels = machine.classify(test_fea)
# Calulate the values for confusion matrix
confusion_matrix = numpy.zeros([num_digits,num_digits])
......
......@@ -95,7 +95,7 @@ def main():
machine = boost_trainer.train(train_fea, train_targets)
# Classify the test samples (testsamp) using the boosted classifier generated above
prediction_labels = machine.classify(test_fea)
pred_scores, prediction_labels = machine.classify(test_fea)
# Calulate the values for confusion matrix
confusion_matrix = numpy.zeros([num_digits,num_digits])
......
......@@ -63,7 +63,7 @@ boost_trainer.lamda = 0.4 # lamda value for variational loss
model = boost_trainer.train(fea_tr, train_targets)
# Classify the test samples (testsamp) using the boosited classifier generated above
prediction_labels = model.classify(fea_ts)
pred_scores, prediction_labels = model.classify(fea_ts)
print prediction_labels[1:10,:]
print test_targets[1:10,:]
accuracy = float(sum(np.sum(prediction_labels == test_targets,1) == num_op))/float(prediction_labels.shape[0])
......
import unittest
import random
import xbob.boosting
import numpy
class TestStumpTrainer(unittest.TestCase):
"""Perform test on stump weak trainer"""
def test_stump_trainer(self):
# test the stump trainer for basic linearly seperable case and check the conditions on stump parameters
trainer = xbob.boosting.core.trainers.StumpTrainer()
n_samples = 100
dim = 5
x_train1 = numpy.random.randn(n_samples, dim) + 4
x_train2 = numpy.random.randn(n_samples, dim) - 4
x_train = numpy.vstack((x_train1, x_train2))
y_train = numpy.hstack((numpy.ones(n_samples),-numpy.ones(n_samples)))
scores = numpy.zeros(2*n_samples)
t = y_train*scores
loss = -y_train*(numpy.exp(y_train*scores))
stump = trainer.compute_weak_trainer(x_train,loss)
self.assertTrue(stump.threshold <= numpy.max(x_train))
self.assertTrue(stump.threshold >= numpy.min(x_train))
self.assertTrue(stump.selected_indices >= 0)
self.assertTrue(stump.selected_indices < dim)
x_test1 = numpy.random.randn(n_samples, dim) + 4
x_test2 = numpy.random.randn(n_samples, dim) - 4
x_test = numpy.vstack((x_test1, x_test2))
y_test = numpy.hstack((numpy.ones(n_samples),-numpy.ones(n_samples)))
prediction = trainer.get_weak_scores(x_test) # return negative labels
self.assertTrue(numpy.all(prediction.T * y_test < 0) )
def test_stump_index(self):
# test the stump trainer if the correct feature indices are selected
trainer = xbob.boosting.core.trainers.StumpTrainer()
num_samples = 100
dim = 10
selected_index = 4
x_train1 = numpy.random.randn(num_samples, dim)
x_train2 = numpy.random.randn(num_samples, dim)
x_train = numpy.vstack((x_train1, x_train2))
x_train[0:num_samples,selected_index] = x_train[0:num_samples,selected_index] +2
x_train[num_samples+1:,selected_index] = x_train[num_samples +1:,selected_index] -2
y_train = numpy.hstack((numpy.ones(num_samples),-numpy.ones(num_samples)))
scores = numpy.zeros(2*num_samples)
t = y_train*scores
loss = -y_train*(numpy.exp(y_train*scores))
stump = trainer.compute_weak_trainer(x_train,loss)
self.assertEqual(stump.selected_indices, selected_index)
polarity = stump.polarity
# test the check on polarity when the labels are reversed
y_train = - y_train
t = y_train*scores
loss = -y_train*(numpy.exp(y_train*scores))
stump = trainer.compute_weak_trainer(x_train,loss)
polarity_rev = stump.polarity
self.assertEqual(polarity, -polarity_rev)
def test_threshold(self):
# test to check the threshold value of the weak trainer
trainer = xbob.boosting.core.trainers.StumpTrainer()
num_samples = 100
dim = 10
selected_index = 4
x_train1 = numpy.random.randn(num_samples, dim)
x_train2 = numpy.random.randn(num_samples, dim)
x_train = numpy.vstack((x_train1, x_train2))
x_train[0:num_samples,selected_index] = x_train[0:num_samples,selected_index] +4
x_train[num_samples+1:,selected_index] = x_train[num_samples +1:,selected_index] +2
y_train = numpy.hstack((numpy.ones(num_samples),-numpy.ones(num_samples)))
scores = numpy.zeros(2*num_samples)
t = y_train*scores
loss = -y_train*(numpy.exp(y_train*scores))
stump = trainer.compute_weak_trainer(x_train,loss)
self.assertTrue(stump.threshold > 2)
self.assertTrue(stump.threshold < 4)
Supports Markdown
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