Skip to content

Commit

Permalink
use std::shared_ptr and drop RefCount
Browse files Browse the repository at this point in the history
drop C prefix
use std::vector for DynamicArray
  • Loading branch information
vigsterkr committed Nov 7, 2019
1 parent 580a3b3 commit 8a5602e
Show file tree
Hide file tree
Showing 1,803 changed files with 32,551 additions and 34,818 deletions.
18 changes: 9 additions & 9 deletions applications/classification/random_fourier_classification.cpp
Expand Up @@ -108,22 +108,22 @@ int main(int argv, char** argc)


/** Creating features */
CBinaryLabels* labels = new CBinaryLabels(label);
BinaryLabels* labels = new BinaryLabels(label);
SG_REF(labels);

CSparseFeatures<float64_t>* s_feats = new CSparseFeatures<float64_t>(sparse_data);
SparseFeatures<float64_t>* s_feats = new SparseFeatures<float64_t>(sparse_data);
SGVector<float64_t> params(1);
params[0] = width;
CRandomFourierDotFeatures* r_feats = new CRandomFourierDotFeatures(
s_feats, D, KernelName::GAUSSIAN, params);


/** Training */
CLibLinear* svm = new CLibLinear(C, r_feats, labels);
//CSVMOcas* svm = new CSVMOcas(C, r_feats, labels);
LibLinear* svm = new LibLinear(C, r_feats, labels);
//SVMOcas* svm = new SVMOcas(C, r_feats, labels);
svm->set_epsilon(epsilon);
SG_SPRINT("Starting training\n");
CTime* timer = new CTime();
Time* timer = new Time();
svm->train();
float64_t secs = timer->cur_runtime_diff_sec();
timer->stop();
Expand All @@ -132,7 +132,7 @@ int main(int argv, char** argc)
/** Training completed */

/** Evaluating */
CBinaryLabels* predicted = svm->apply()->as<CBinaryLabels>();
BinaryLabels* predicted = svm->apply()->as<BinaryLabels>();
CPRCEvaluation* prc_evaluator = new CPRCEvaluation();
CROCEvaluation* roc_evaluator = new CROCEvaluation();
CAccuracyMeasure* accuracy_evaluator = new CAccuracyMeasure();
Expand All @@ -152,11 +152,11 @@ int main(int argv, char** argc)
sparse_data = load_data(testpath, label_vec);
label = SGVector<float64_t>(label_vec, sparse_data.num_vectors);

s_feats = new CSparseFeatures<float64_t>(sparse_data);
s_feats = new SparseFeatures<float64_t>(sparse_data);
r_feats = new CRandomFourierDotFeatures(s_feats, D, KernelName::GAUSSIAN, width, w);
CBinaryLabels* test_labels = new CBinaryLabels(label);
BinaryLabels* test_labels = new BinaryLabels(label);

predicted = svm->apply(r_feats)->as<CBinaryLabels>();
predicted = svm->apply(r_feats)->as<BinaryLabels>();
auROC = roc_evaluator->evaluate(predicted, test_labels);
auPRC = prc_evaluator->evaluate(predicted, test_labels);
accuracy = accuracy_evaluator->evaluate(predicted, test_labels);
Expand Down
2 changes: 1 addition & 1 deletion configs/valgrind.supp
Expand Up @@ -127,5 +127,5 @@
Memcheck:Addr8
fun:__GI___strncasecmp_l
fun:____strtod_l_internal
fun:*CParser*read_real*
fun:*Parser*read_real*
}
18 changes: 9 additions & 9 deletions doc/OpenCV_docs/OpenCV_KNN_vs_Shogun_KNN.md
Expand Up @@ -158,41 +158,41 @@ Then, evaluate the accuracy using the ```opencv_trainresponse``` Mat.
cout << "The accuracy of OpenCV's k-NN is: " << 100.0 * ko/testdata.rows << endl;
```
We, as usual, prepare the ```CDenseFeatures``` object namely ```shogun_trainfeatures``` for training the **Shogun** k-NN over it.
We, as usual, prepare the ```DenseFeatures``` object namely ```shogun_trainfeatures``` for training the **Shogun** k-NN over it.
```CPP
SGMatrix<float64_t> shogun_traindata = CV2SGFactory::get_sgmatrix<float64_t>(traindata);
shogun_traindata = linalg::transpose_matrix(shogun_traindata);
CDenseFeatures<float64_t>* shogun_trainfeatures = new CDenseFeatures<float64_t>(shogun_traindata);
auto shogun_trainfeatures = std::make_shared<DenseFeatures<float64_t>>(shogun_traindata);
```

We form the ```CMulticlassLabels``` object named ```labels``` for containing the responses from the ```shogun_trainresponse``` Mat.
We form the ```MulticlassLabels``` object named ```labels``` for containing the responses from the ```shogun_trainresponse``` Mat.
```CPP
CDenseFeatures<float64_t>* shogun_dense_response =
DenseFeatures<float64_t>* shogun_dense_response =
CV2SGFactory::get_dense_features<float64_t>(shogun_trainresponse);
SGVector<float64_t> shogun_vector_response = shogun_dense_response->get_feature_vector(0);
CMulticlassLabels* labels = new CMulticlassLabels(shogun_vector_response);
MulticlassLabels* labels = new MulticlassLabels(shogun_vector_response);
```
We, as usual, prepare the ```CDenseFeatures``` object namely ```shogun_testfeatures``` for testing.
We, as usual, prepare the ```DenseFeatures``` object namely ```shogun_testfeatures``` for testing.
```CPP
SGMatrix<float64_t> shogun_testdata = CV2SGFactory::get_sgmatrix<float64_t>(testdata);
shogun_testdata = linalg::transpose_matrix(shogun_testdata);
CDenseFeatures<float64_t>* shogun_testfeatures = new CDenseFeatures<float64_t>(shogun_testdata);
DenseFeatures<float64_t>* shogun_testfeatures = new DenseFeatures<float64_t>(shogun_testdata);
```
___
**Shogun's** k-NN implementation.
```CPP
// Create k-NN classifier.
CKNN* knn = new CKNN(k, new CEuclideanDistance(shogun_trainfeatures, shogun_trainfeatures), labels);
KNN* knn = new KNN(k, new EuclideanDistance(shogun_trainfeatures, shogun_trainfeatures), labels);

// Train classifier.
knn->train();
```

Test it!
```CPP
CMulticlassLabels* output = knn->apply_multiclass(shogun_testfeatures);
MulticlassLabels* output = knn->apply_multiclass(shogun_testfeatures);
SGMatrix<int32_t> multiple_k_output = knn->classify_for_multiple_k();
SGVector<float64_t> sgvec = output->get_labels();

Expand Down
24 changes: 12 additions & 12 deletions doc/OpenCV_docs/OpenCV_NN_vs_Shogun_NN.md
Expand Up @@ -224,29 +224,29 @@ As usual, we start with creating a ```DenseFeatures``` object with the training
```CPP
SGMatrix<float64_t> shogun_traindata = CV2SGFactory::get_sgmatrix<float64_t>(traindata);
shogun_traindata = linalg::transpose_matrix(shogun_traindata);
CDenseFeatures<float64_t>* shogun_trainfeatures = new CDenseFeatures<float64_t>(shogun_traindata);
auto shogun_trainfeatures = std::make_shared<DenseFeatures<float64_t>>(shogun_traindata);
```


The training responses are in an object of type ```MulticlassLabels```.
```CPP
CDenseFeatures<float64_t>* shogun_dense_response = CV2SGFactory::get_dense_features<float64_t>(shogun_trainresponse);
auto shogun_dense_response = CV2SGFactory::get_dense_features<float64_t>(shogun_trainresponse);
SGVector<float64_t> shogun_vector_response = shogun_dense_response->get_feature_vector(0);
CMulticlassLabels* labels = new CMulticlassLabels(shogun_vector_response);
auto labels = std::make_shared<MulticlassLabels>(shogun_vector_response);
```
Prepare the testing data.
```CPP
SGMatrix<float64_t> shogun_testdata = CV2SGFactory::get_sgmatrix<float64_t>(testdata);
shogun_testdata = linalg::transpose_matrix(shogun_testdata);
CDenseFeatures<float64_t>* testfeatures = new CDenseFeatures<float64_t>(shogun_testdata);
auto testfeatures = std::make_shared<CDenseFeatures<float64_t>>(shogun_testdata);
```


To use Neural Networks in **Shogun** the following things need to be done:

* Prepare a ```CDynamicObjectArray``` of ```CNeuralLayer```-based objects that specify the type of layers used in the network. The array must contain at least one input layer. The last layer in the array is treated as the output layer. Also note that forward propagation is performed in the order the layers appear in the array. So, if layer ```j``` takes its input from layer ```i```, then ```i``` must be less than ```j```.
* Prepare a ```DynamicObjectArray``` of ```NeuralLayer```-based objects that specify the type of layers used in the network. The array must contain at least one input layer. The last layer in the array is treated as the output layer. Also note that forward propagation is performed in the order the layers appear in the array. So, if layer ```j``` takes its input from layer ```i```, then ```i``` must be less than ```j```.

* Specify how the layers are connected together. This can be done using either ```connect()``` or ```quick_connect()```.

Expand All @@ -264,23 +264,23 @@ To use Neural Networks in **Shogun** the following things need to be done:
---
* Let us start with the first step.

We will be preparing a ```CDynamicObjectArray```. It creates an array that can be used like a list or an array.
We will be preparing a ```DynamicObjectArray```. It creates an array that can be used like a list or an array.
We then append information related to the number of neurons per layer in the respective order.

Here I have created a ```3``` layered network. The input layer consists of ```6``` neurons which is equal to number of features.
The hidden layer has ```10``` neurons and similarly the output layer has ```4``` neurons which is equal to the number of classes.

```CPP
CDynamicObjectArray* layers = new CDynamicObjectArray();
layers->append_element(new CNeuralInputLayer(6));
layers->append_element(new CNeuralLogisticLayer(10));
layers->append_element(new CNeuralLogisticLayer(4));
DynamicObjectArray* layers = new DynamicObjectArray();
layers->append_element(new NeuralInputLayer(6));
layers->append_element(new NeuralLogisticLayer(10));
layers->append_element(new NeuralLogisticLayer(4));
```
* Here we have to make a connection between the three layers that we formed above. To connect each neuron of one layer to each one of the layer suceeding it, we can directly use ```quick_connect()```. However If particular connections are to be made separately, we may have to use ```connect()```.
```CPP
CNeuralNetwork* network = new CNeuralNetwork(layers);
NeuralNetwork* network = new NeuralNetwork(layers);
network->quick_connect();
```

Expand Down Expand Up @@ -309,7 +309,7 @@ The hidden layer has ```10``` neurons and similarly the output layer has ```4```
* Test it!
```CPP
CMulticlassLabels* predictions = network->apply_multiclass(testfeatures);
MulticlassLabels* predictions = network->apply_multiclass(testfeatures);
k=0;
for (int32_t i=0; i<mytraindataidx.cols; i++ )
{
Expand Down
12 changes: 6 additions & 6 deletions doc/OpenCV_docs/OpenCV_SVM_vs_Shogun_SVM.md
Expand Up @@ -193,20 +193,20 @@ We start with creating the training data as the ```DenseFeatures```.
```CPP
SGMatrix<float64_t> shogun_traindata = CV2SGFactory::get_sgmatrix<float64_t>(traindata);
shogun_traindata = linalg::transpose_matrix(shogun_traindata);
CDenseFeatures<float64_t>* shogun_trainfeatures = new CDenseFeatures<float64_t>(shogun_traindata);
DenseFeatures<float64_t>* shogun_trainfeatures = new DenseFeatures<float64_t>(shogun_traindata);
```

Now the training responses as the ```MulticlassLabels```.
```CPP
CDenseFeatures<float64_t>* shogun_dense_response = CV2SGFactory::get_dense_features<float64_t>(trainresponse);
DenseFeatures<float64_t>* shogun_dense_response = CV2SGFactory::get_dense_features<float64_t>(trainresponse);
SGVector<float64_t> shogun_vector_response = shogun_dense_response->get_feature_vector(0);
CMulticlassLabels* labels = new CMulticlassLabels(shogun_vector_response);
MulticlassLabels* labels = new MulticlassLabels(shogun_vector_response);
```
Now the Kernel.
```CPP
CLinearKernel* kernel = new CLinearKernel();
LinearKernel* kernel = new LinearKernel();
kernel->init(shogun_trainfeatures, shogun_trainfeatures);
```

Expand All @@ -222,12 +222,12 @@ Prepare the testing data.
```CPP
SGMatrix<float64_t> shogun_testdata=CV2SGFactory::get_sgmatrix<float64_t>(testdata);
shogun_testdata = linalg::transpose_matrix(shogun_testdata);
CDenseFeatures<float64_t>* testfeatures=new CDenseFeatures<float64_t>(shogun_testdata);
DenseFeatures<float64_t>* testfeatures=new DenseFeatures<float64_t>(shogun_testdata);
```

Testing Procedure.
```CPP
CMulticlassLabels* results=shogunsvm->apply_multiclass(testfeatures);
MulticlassLabels* results=shogunsvm->apply_multiclass(testfeatures);
k=0;
for(int i=0; i<testdata.rows; i++)
{
Expand Down
10 changes: 5 additions & 5 deletions doc/OpenCV_docs/eigenfaces.cpp
Expand Up @@ -104,9 +104,9 @@ int main()
// ]


// convert the Stacked_mats into the CDenseFeatures of Shogun.
// convert the Stacked_mats into the DenseFeatures of Shogun.
// From here on we will be performing our PCA algo in Shogun.
CDenseFeatures<float64_t>* Face_features=new CDenseFeatures<float64_t>(Stacked_mats);
DenseFeatures<float64_t>* Face_features=new DenseFeatures<float64_t>(Stacked_mats);
SG_REF(Face_features);

// We initialise the Preprocessor CPCA of Shogun which performs principal
Expand Down Expand Up @@ -195,19 +195,19 @@ int main()

// we need to have the Densefeature pointer of the finalmat.
// finalmat_densefeature_ptr is the lhs.
CDenseFeatures<float64_t>* finalmat_densefeature_ptr=new CDenseFeatures<float64_t>(finalmat);
DenseFeatures<float64_t>* finalmat_densefeature_ptr=new DenseFeatures<float64_t>(finalmat);
SG_REF(finalmat_densefeature_ptr);

// and similarly we just need to convert the testimage_sgvec into the DenseFeature pointer for the rhs.
SGMatrix<float64_t>data_matrix(testimage_projected_vec.vlen, 1);

CDenseFeatures<float64_t>* testimage_dense=new CDenseFeatures<float64_t>(data_matrix);
DenseFeatures<float64_t>* testimage_dense=new DenseFeatures<float64_t>(data_matrix);
SG_REF(testimage_dense);
testimage_dense->set_feature_vector(testimage_projected_vec,0);



CEuclideanDistance* euclid=new CEuclideanDistance(finalmat_densefeature_ptr,testimage_dense);
EuclideanDistance* euclid=new EuclideanDistance(finalmat_densefeature_ptr,testimage_dense);
SG_REF(euclid);
float64_t distance_array[size];
int min_index=0;
Expand Down
22 changes: 11 additions & 11 deletions doc/OpenCV_docs/fisherfaces.cpp
Expand Up @@ -99,9 +99,9 @@ int main()
// [ Img1[L] Img2[L] Img3[L] Img4[L] ...... ImgS[L];]here L = length and S = size.
// ]

// convert the Stacked_mats into the CDenseFeatures of Shogun.
// convert the Stacked_mats into the DenseFeatures of Shogun.
// From here on we will be performing our PCA algo in Shogun.
CDenseFeatures<float64_t>* Face_features=new CDenseFeatures<float64_t>(Stacked_mats);
DenseFeatures<float64_t>* Face_features=new DenseFeatures<float64_t>(Stacked_mats);
SG_REF(Face_features)

// We initialise the Preprocessor CPCA of Shogun which performs principal
Expand Down Expand Up @@ -140,15 +140,15 @@ int main()
// So in effect we just reduced the dimensions of each of our
// training images. We will further reduce it using LDA.

// Convert the Labels in the form of CMulticlassLabels
// Convert the Labels in the form of MulticlassLabels
SGVector<float64_t> labels_vector(size);
for (int i=0;i<size;i++)
labels_vector[i]=labels[i];
CMulticlassLabels* actual_labels=new CMulticlassLabels(labels_vector);
MulticlassLabels* actual_labels=new MulticlassLabels(labels_vector);
SG_REF(actual_labels);

CDenseFeatures<float64_t>* pca_dense_feat=
new CDenseFeatures<float64_t>(pca_projection);
DenseFeatures<float64_t>* pca_dense_feat=
new DenseFeatures<float64_t>(pca_projection);
SG_REF(pca_dense_feat);

// Applying the classical Fisher LDA algorithm.
Expand Down Expand Up @@ -216,17 +216,17 @@ int main()

// we need to have the Densefeature pointer of the lda_projection.
// It is the lhs.
CDenseFeatures<float64_t>* lhs=
new CDenseFeatures<float64_t>(lda_projection);
DenseFeatures<float64_t>* lhs=
new DenseFeatures<float64_t>(lda_projection);

// and similarly we just need to convert the testimage_sgvec into the
// DenseFeature pointer for the rhs.
SGMatrix<float64_t>data_matrix(testimage_projected_vec.vlen, 1);
CDenseFeatures<float64_t>* rhs=
new CDenseFeatures<float64_t>(data_matrix);
DenseFeatures<float64_t>* rhs=
new DenseFeatures<float64_t>(data_matrix);
rhs->set_feature_vector(testimage_projected_vec,0);

CEuclideanDistance* euclid=new CEuclideanDistance(lhs, rhs);
EuclideanDistance* euclid=new EuclideanDistance(lhs, rhs);
SG_REF(euclid);
float64_t distance_array[size];
int min_index=0;
Expand Down
4 changes: 2 additions & 2 deletions doc/cookbook/source/examples/base_api/dense_dispatching.rst
Expand Up @@ -3,15 +3,15 @@ Dense feature type dispatching
==============================

:sgclass:`DenseFeatures`, like those read from a :sgclass:`CCSVFIle`, can be read into memory in multiple primitive types, e.g. 32 bit or 64 bit floating point numbers.
All algorithms that inherit from :sgclass:`CDenseRealDispatch` downstream can deal with multiples of such types, and they will carry out required computations in the corresponding primitive type.
All algorithms that inherit from :sgclass:`DenseRealDispatch` downstream can deal with multiples of such types, and they will carry out required computations in the corresponding primitive type.
Reducing from 64 bit float to 32 bit float can be beneficial if for example very large matrices have to be stored.

-------
Example
-------

Imagine we have files with training data.
We create 64 bit and 32 bit float :sgclass:`CDenseFeatures` of appropriate primitive type and also create :sgclass:`CBinaryLabels` as
We create 64 bit and 32 bit float :sgclass:`DenseFeatures` of appropriate primitive type and also create :sgclass:`BinaryLabels` as

.. sgexample:: dense_dispatching.sg:create_features

Expand Down
6 changes: 3 additions & 3 deletions doc/cookbook/source/examples/binary/averaged_perceptron.rst
Expand Up @@ -29,8 +29,8 @@ See chapter 17 in :cite:`barber2012bayesian` for a brief explanation of the Perc
Example
-------

Given a linearly separable dataset, we create some CDenseFeatures
(RealFeatures, here 64 bit float values) and some :sgclass:`CBinaryLabels` to set up the training and validation sets.
Given a linearly separable dataset, we create some DenseFeatures
(RealFeatures, here 64 bit float values) and some :sgclass:`BinaryLabels` to set up the training and validation sets.

.. sgexample:: averaged_perceptron.sg:create_features

Expand All @@ -39,7 +39,7 @@ We also set its learn rate and its maximum iterations.

.. sgexample:: averaged_perceptron.sg:set_parameters

Then we train the :sgclass:`CAveragedPerceptron` and we apply it to the test data, which gives the predicted :sgclass:`CBinaryLabels`.
Then we train the :sgclass:`CAveragedPerceptron` and we apply it to the test data, which gives the predicted :sgclass:`BinaryLabels`.

.. sgexample:: averaged_perceptron.sg:train_and_apply

Expand Down
Expand Up @@ -16,24 +16,24 @@ subject to:
where :math:`N` is the number of training samples, :math:`{\bf x}_i` are training samples, :math:`k` is a kernel, :math:`\alpha_i` are the weights, :math:`y_i` is the corresponding label where :math:`y_i \in \{-1,+1\}` and :math:`C` is a pre-specified regularization parameter.

This example uses LibSVM :cite:`chang2011libsvm` as backend, Shogun has many more SVM implementations, see :sgclass:`CSVM`.
This example uses LibSVM :cite:`chang2011libsvm` as backend, Shogun has many more SVM implementations, see :sgclass:`SVM`.

See :cite:`scholkopf2002learning` and Chapter 6 in :cite:`cristianini2000introduction` for a detailed introduction.

-------
Example
-------

Imagine we have files with training and test data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CBinaryLabels` as
Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`BinaryLabels` as

.. sgexample:: kernel_support_vector_machine.sg:create_features

In order to run :sgclass:`CLibSVM`, we first need to initialize a :sgclass:`CKernel` instance, such as :sgclass:`CGaussianKernel`.
We then create a :sgclass:`CKernelMachine` instance, here :sgclass:`CLibSVM`, and provide it with parameters like the regularization coefficient :math:`C`, the kernel, the training labels, and an optional residual convergence parameter epsilon.
In order to run :sgclass:`CLibSVM`, we first need to initialize a :sgclass:`Kernel` instance, such as :sgclass:`GaussianKernel`.
We then create a :sgclass:`KernelMachine` instance, here :sgclass:`CLibSVM`, and provide it with parameters like the regularization coefficient :math:`C`, the kernel, the training labels, and an optional residual convergence parameter epsilon.

.. sgexample:: kernel_support_vector_machine.sg:create_instance

Then we train it on training data and apply it to test data. This gives :sgclass:`CLabels`, which we can extract the label vector from.
Then we train it on training data and apply it to test data. This gives :sgclass:`Labels`, which we can extract the label vector from.

.. sgexample:: kernel_support_vector_machine.sg:train_and_apply

Expand Down

0 comments on commit 8a5602e

Please sign in to comment.